Python - 在 asyncio 中 运行 函数的正确方法是什么?
Python - What is the right way to run functions in asyncio?
我正在使用 FastApi 并且有一个端点。
我有两个长 运行ning 函数,我想 运行 同时使用 asyncio
因此,我创建了两个函数:
async def get_data_one():
return 'done_one'
async def get_data_two():
return 'done_two'
这些函数从外部网络服务获取数据。
我想同时执行它们,所以我创建了另一个函数来执行它:
async def get_data():
loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
task_1 = loop.create_task(get_data_one)
task_2 = loop.create_task(get_data_two)
tasks = (task_1, task_2)
first, second = loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
# I will then perform cpu intensive computation on the results
# for now - assume i am just concatenating the results
return first + second
最后,我有了终点:
@app.post("/result")
async def get_result_async():
r = await get_data()
return r
即使是这个简单的示例也会中断,当我到达终点时出现以下异常:
RuntimeError: 这个事件循环已经 运行ning
错误:从未检索到 _GatheringFuture 异常
未来:<_GatheringFuture 完成异常=AttributeError("'function' 对象没有属性 'send'",)>
AttributeError: 'function' 对象没有属性 'send'
这是一个简化的代码,但我真的很感激如何以正确的方式做到这一点。
在 FastAPI 上下文中,您永远不需要 运行 异步循环;只要您的服务器进程存在,它总是 运行ning。
因此,您只需要
import asyncio
async def get_data_one():
return "done_one"
async def get_data_two():
return "done_two"
async def get_data():
a, b = await asyncio.gather(get_data_one(), get_data_two())
return a + b
#@route decorator here...
async def get_result_async():
r = await get_data()
print("Get_data said:", r)
return r
# (this is approximately what is done under the hood,
# presented here to make this a self-contained example)
asyncio.run(get_result_async())
就这么简单:
async def get_data():
first, second = await asyncio.gather(
get_data_one(),
get_data_two(),
)
return first + second
我正在使用 FastApi 并且有一个端点。
我有两个长 运行ning 函数,我想 运行 同时使用 asyncio
因此,我创建了两个函数:
async def get_data_one():
return 'done_one'
async def get_data_two():
return 'done_two'
这些函数从外部网络服务获取数据。
我想同时执行它们,所以我创建了另一个函数来执行它:
async def get_data():
loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
task_1 = loop.create_task(get_data_one)
task_2 = loop.create_task(get_data_two)
tasks = (task_1, task_2)
first, second = loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
# I will then perform cpu intensive computation on the results
# for now - assume i am just concatenating the results
return first + second
最后,我有了终点:
@app.post("/result")
async def get_result_async():
r = await get_data()
return r
即使是这个简单的示例也会中断,当我到达终点时出现以下异常:
RuntimeError: 这个事件循环已经 运行ning 错误:从未检索到 _GatheringFuture 异常 未来:<_GatheringFuture 完成异常=AttributeError("'function' 对象没有属性 'send'",)> AttributeError: 'function' 对象没有属性 'send'
这是一个简化的代码,但我真的很感激如何以正确的方式做到这一点。
在 FastAPI 上下文中,您永远不需要 运行 异步循环;只要您的服务器进程存在,它总是 运行ning。
因此,您只需要
import asyncio
async def get_data_one():
return "done_one"
async def get_data_two():
return "done_two"
async def get_data():
a, b = await asyncio.gather(get_data_one(), get_data_two())
return a + b
#@route decorator here...
async def get_result_async():
r = await get_data()
print("Get_data said:", r)
return r
# (this is approximately what is done under the hood,
# presented here to make this a self-contained example)
asyncio.run(get_result_async())
就这么简单:
async def get_data():
first, second = await asyncio.gather(
get_data_one(),
get_data_two(),
)
return first + second