响应用户并继续运行异步功能

时间:2016-11-23 15:16:06

标签: aiohttp

我想在收到请求后立即回复用户,然后继续将数据保存到数据库

async def index(request):
    data = await request.post()
    response.write("done")
    async with request.app.mysqlpool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("INSERT INTO ...")

如果我尝试将request.write之后的部分包装为单独的函数而不是用run_in_executor调用它 - 它不允许在内部使用等待函数(TypeError:coroutines不能与run_in_executor()一起使用)

有没有办法在aiohttp中做到这一点?

1 个答案:

答案 0 :(得分:1)

await response.drain()来电之后推送response.write(...)

它并不能完全保证数据是通过网络发送的,但它是最接近的解决方案。

相关问题