python runtime_error ai_generated true

DeprecationWarning: There is no current event loop

ID: python/asyncio-loop-policy-deprecated

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-07-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active — — —
3.11 active — — —
3.12 active — — —

Root Cause

asyncio.get_event_loop() was called with no running loop and no loop set for the current thread; Python 3.10+ deprecates this behavior and 3.12+ warns.

generic

中文

在没有运行循环且当前线程未设置循环的情况下调用了 asyncio.get_event_loop();Python 3.10+ 弃用此行为,3.12+ 发出警告。

Workarounds

  1. 95% success
    Inside async code, use asyncio.get_running_loop():
    
    loop = asyncio.get_running_loop()
  2. 90% success
    From sync code, use asyncio.new_event_loop() and manage lifetime explicitly:
    
    loop = asyncio.new_event_loop()
    try:
        loop.run_until_complete(coro())
    finally:
        loop.close()
  3. 92% success
    Use asyncio.Runner (3.11+) for repeated runs:
    
    with asyncio.Runner() as runner:
        runner.run(coro())

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Hides the warning but the underlying API will be removed in 3.14.

  2. 65% fail

    Creates a loop that is never closed; leaks resources and causes 'loop is closed' errors later.

  3. 90% fail

    Not a maintainable fix; 3.9 is EOL in Oct 2025.