本文介绍了我如何不等待就调用异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在aiohttp应用程序中有一个控制器操作。
I have a controller action in aiohttp application.
async def handler_message(request):
try:
content = await request.json()
perform_message(x,y,z)
except (RuntimeError):
print("error in perform fb message")
finally:
return web.Response(text="Done")
perform_message
是异步功能。现在,当我调用操作时,我希望我的操作尽快返回并在事件循环中放入 perform_message
。
perform_message
is async function. Now, when I call action I want that my action return as soon as possible and perform_message
put in event loop.
通过这种方式,不会执行 perform_message
In this way, perform_message
isn't executed
推荐答案
将使用函数:
One way would be to use create_task
function:
import asyncio
async def handler_message(request):
...
loop = asyncio.get_event_loop()
loop.create_task(perform_message(x,y,z))
...
这篇关于我如何不等待就调用异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!