一个在asyncio事件循环中运行异步任务的concurrent.futures.Executor实现。
项目描述
aio-executor
一个在asyncio循环中运行异步任务的concurrent.futures.Executor实现。
示例用法
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'])
为了方便,还提供了一个run_with_asyncio
装饰器。这个装饰器在AioExecutor
实例中运行装饰的异步函数。
以下示例展示了如何使用此装饰器实现Flask框架的异步视图函数
@app.route('/')
@run_with_asyncio
async def index():
return await get_random_quote()
如何安装
pip install aio-executor