Combining loop.run_until_complete and asnycio.gather results in RuntimeError: This event loop is already running

Combining loop.run_until_complete and asnycio.gather results in RuntimeError: This event loop is already running

我想运行两个并行的异步函数。

这是我的代码:

import asyncio

async def count(label, number):
    for i in range(number):
        print(label, i)
        await asyncio.sleep(1)

async def main():
    asyncio.get_event_loop().run_until_complete(
        asyncio.gather(count("a", 3), count("b", 5))
    )

asyncio.run(main())

这是我看到的错误:

$ python3 test.py 
a 0
b 0
Traceback (most recent call last):
  File "test.py", line 188, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "test.py", line 167, in main
    asyncio.get_event_loop().run_until_complete(
  File "/usr/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete
    self._check_running()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 578, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
_GatheringFuture exception was never retrieved
future: <_GatheringFuture finished exception=CancelledError()>
Traceback (most recent call last):
  File "test.py", line 188, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "test.py", line 167, in main
    asyncio.get_event_loop().run_until_complete(
  File "/usr/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete
    self._check_running()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 578, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/asyncio/tasks.py", line 654, in sleep
    return await future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

asyncio.exceptions.CancelledError

我不明白出了什么问题。最奇怪的是,我认为这段代码以前是有效的?那是怎么回事?我目前在 Debian 11 上使用 Python 3.9。也许我需要尝试旧版本以查看是否确实存在差异。

我发现了我的错误。将 loop.run_until_complete 与 asnycio.gather 结合起来不是问题。我也错误地将 asyncio.get_event_loop().run_until_complete 放在异步函数中。这也是为什么它说“事件循环已经 运行”。

这个有效:

import asyncio

async def count(label, number):
    for i in range(number):
        print(label, i)
        await asyncio.sleep(1)

def main():
    asyncio.get_event_loop().run_until_complete(
        asyncio.gather(count("a", 3), count("b", 5))
    )

main()