如何在不等待的情况下调用异步函数? [英] How I call an async function without await?

查看:42
本文介绍了如何在不等待的情况下调用异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 是异步函数.现在,当我调用 action 时,我希望我的 action 尽快返回并将 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

推荐答案

一种方法是使用 create_task 函数:

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))
    ...

这篇关于如何在不等待的情况下调用异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆