A concurrent.futures.Executor implementation that runs asynchronous tasks in an asyncio loop.
Example usage:
from aio_executor import AioExecutor
async def my_async_function(arg):
# ...
with AioExecutor() as aioexec:
# single invocation
f = aioexec.submit(my_async_function, 'foo')
result = f.result()
# multiple concurrent invocations using "map"
results = aioexec.map(my_async_function, ['foo', 'bar', 'baz'])
As a convenience, a run_with_asyncio
decorator is also provided. This
decorator runs the decorated async function in a AioExecutor
instance.
The example below shows how to implement an async view function for the Flask framework using this decorator:
@app.route('/')
@run_with_asyncio
async def index():
return await get_random_quote()
pip install aio-executor
The idea of implementing an Executor
instance based on asyncio is apparently
not that original. I initially attempted to register this package on PyPI as
"asyncio-executor" and found that the name was already taken.
Below is the list of fairly similar implementations I know about. If for any reason my version does not work for you, be sure to try these others out.