Fabricio Reinert
Fabricio Reinert

Reputation: 33

Create my own Coroutines to use with asyncio

Let's say i'm not interested to relay on 3rd party modules. Is it possible to wrap a standard python module into a coroutine in order to use with asyncio?

For example:

something like this:

@asyncio.coroutine
def async_open(filename: str, mode: str) -> str:
    with open(filename, mode) as fopen:
        for block in fopen:
            yield block

Upvotes: 1

Views: 294

Answers (1)

Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39636

Usually you can cast some sync code to async running it asynchronously using run_in_executor.

Note, that casting sync code to async this way means it wouldn't block event loop and other coroutines, but it doesn't always mean such code would run faster then original. For example, if you want to cast some CPU-bound python code to coroutine you'll get benefit only if you run it in multiple processes on multiple cores. But if you cast some network I/O related python code, using threads would be fine to achieve parallelization.

open() to work with files already casted to coroutine using threads executor in aiofiles module. You can look at module's source code and implement your own wrapper same way.

To see how to wrap urllib take a look at this answer. It shows casting sync requests.get to coroutine: everything would be same for urllib.

Upvotes: 1

Related Questions