我有一个fastAPI应用程序,可以发布两个请求,其中一个更长(如果有帮助的话,它们是Elasticsearch查询,我正在使用AsyncElasticsearch模块,该模块已经返回协同路由)。这是我的尝试:
class my_module:
search_object = AsyncElasticsearch(url, port)
async def do_things(self):
resp1 = await search_object.search() #the longer one
print(check_resp1)
resp2 = await search_object.search() #the shorter one
print(check_resp2)
process(resp2)
process(resp1)
do_synchronous_things()
return thing
app = FastAPI()
@app.post("/")
async def service(user_input):
result = await my_module.do_things()
return results
我观察到的不是等待resp1
,而是等到它到达check_resp1
时,它已经是一个完整的响应,好像我根本没有使用异步
我是python异步的新手,我知道我的代码不会工作,但我不知道如何修复它。据我所知,当解释器看到await
时,它会启动函数,然后继续,在这种情况下,它会立即发出下一个请求。我该怎么做
是的,这是正确的。在结果准备好之前,协同程序不会继续。您可以使用asyncio.gather并发运行任务:
测试:
或者,您可以使用asyncio.as_completed获得最早的下一个结果:
更新星期五4月2日09:25:33 UTC 2021:
asyncio.run从Python 3.7+开始可用,在以前的版本中,您必须手动创建并启动循环:
解释
代码同步运行的原因是在
do_things
函数中,代码按如下方式执行:search_object.search()
执行search_object.search()
完成并获得结果search_object.search()
执行search_object.search()
完成并获得结果process(resp2)
process(resp1)
do_synchronous_things()
您的目的是使步骤1和3在步骤2和步骤4之前执行。您可以使用
unsync
库轻松地实现它-这里是the documentation你怎么能解决这个问题
更多信息
如果您想了解有关asyncio和asyncronyous编程的更多信息,我建议您使用此tutorial。还有类似的情况,您提供了一些可能的解决方案,以使协同路由同时运行
很明显,我无法运行这段代码,所以您必须自己调试它
相关问题 更多 >
编程相关推荐