Python 3.10 asyncio.gather() 显示 DeprecationWarning: There is no current event loop

Python 3.10 asyncio.gather() shows DeprecationWarning: There is no current event loop

我有一个 Django 应用程序,在它的一个视图中,我使用 asyncio 来向外部组件发出一些并发请求。

想法是这样的:

import asyncio


async def do_request(project):
    result = ...

    return result

def aggregate_results(projects: list):
    loop = asyncio.new_event_loop()

    asyncio.set_event_loop(loop)

    results = loop.run_until_complete(
        asyncio.gather(*(do_request(project) for project in projects))
    )

    loop.close()

    return zip(projects, results)

好吧,当我 运行 测试时,我在这一行得到 DeprecationWarning: There is no current event loop

        asyncio.gather(*(do_request(project) for project in projects))

我应该如何解释这个警告以及我需要更改什么才能摆脱它?谢谢!

根据 documentation,发生这种情况是因为在您调用 gather 时没有事件循环 运行

Deprecated since version 3.10: Deprecation warning is emitted if no positional arguments are provided or not all positional arguments are Future-like objects and there is no running event loop.

您可能已经注意到,您的代码有效。它将继续工作,只要您使用 3.10,您就可以忽略弃用警告。不过,在未来的某个时候,这可能会变成运行时错误。

请稍等片刻,the recommended way to run an event loop is with run,而不是 loop.run_until_complete

def aggregate_results(projects: list):
    results = asyncio.run(asyncio.gather(*(do_request(project) for project in projects)))
    return zip(projects, results)

然而,这实际上行不通。你会得到一个例外

ValueError: a coroutine was expected, got <_GatheringFuture pending>

解决方法是等待来自另一个协同程序的 gather

async def get_project_results(projects: list):
    results = await asyncio.gather(*(do_request(project) for project in projects))
    return results

def aggregate_results(projects: list):
    results = asyncio.run(get_project_results(projects))
    return zip(projects, results)

(您也可以将 get_project_results 与您的 aggregate_results 版本一起使用。)