简体   繁体   English

结合 loop.run_until_complete 和 asnycio.gather 导致 RuntimeError:此事件循环已经在运行

[英]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.

相关问题 Asyncio loop.run_until_complete 在小部件视图之前运行 - Asyncio loop.run_until_complete running before widget view 递归函数上的Asyncio loop.run_until_complete() - Asyncio loop.run_until_complete() on recursive function asyncio loop.run_until_complete 不与 asyncio.sleep 一起睡觉 - asyncio loop.run_until_complete not sleeping with asyncio.sleep 当loop.run_until_complete工作时asyncio.run失败 - asyncio.run fails when loop.run_until_complete works 我如何在asyncio上异步运行`loop.run_until_complete()`本身? - How can I get to run `loop.run_until_complete()` itself asynchronously on asyncio? 在单个 API 调用中组合 asyncio.ensure_future 和 loop.run_until_complete? - Combine asyncio.ensure_future and loop.run_until_complete in a single api call? loop.run_until_complete(g) 出错 An asyncio.Future, a coroutine or an awaitable is required - loop.run_until_complete(g) got error of An asyncio.Future, a coroutine or an awaitable is required loop.run_until_complete 给出“TypeError: An asyncio.Future, a coroutine or an awaitable is required” - loop.run_until_complete gives “TypeError: An asyncio.Future, a coroutine or an awaitable is required” 使用 discord.py 调用 python asyncio loop.run_until_complete() 不起作用? - Calling python asyncio loop.run_until_complete() with discord.py not working? asyncio/aiohttp - create_task() 阻止事件循环,在“此事件循环已在运行”中收集结果 - asyncio/aiohttp - create_task() blocks event loop, gather results in “This event loop is already running ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM