[英]Combining loop.run_until_complete and asnycio.gather results in RuntimeError: This event loop is already running
I want to run two async functions in parallel.我想并行运行两个异步函数。
This is my code:这是我的代码:
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())
And this is the error I'm seeing:这是我看到的错误:
$ 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
I don't understand what's going wrong.我不明白出了什么问题。 And the weirdest part is, that I think this code was working before?最奇怪的是,我认为这段代码以前有效? What's up with that?那是怎么回事? I'm currently using Python 3.9 on Debian 11. Maybe I need to try an older version to see if there is actually a difference.我目前在 Debian 11 上使用 Python 3.9。也许我需要尝试一个旧版本,看看是否真的有区别。
🤦 I found my mistake. 🤦我发现我的错误。 Combining loop.run_until_complete with asnycio.gather was not the problem.将 loop.run_until_complete 与 asnycio.gather 结合起来不是问题。 I mistakenly put the asyncio.get_event_loop().run_until_complete
inside an async function, too.我也错误地将asyncio.get_event_loop().run_until_complete
放在了异步函数中。 That's also why it said that the "event loop is already running".这也是为什么它说“事件循环已经在运行”的原因。
This works:这有效:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.