← All notes

Python asyncio event loop: run tasks, callbacks, and I/O

Answer: The event loop is the core of every asyncio application, running asynchronous tasks and callbacks, network I/O operations, and subprocesses.

Who should call loop methods

Application developers should typically stay with high-level asyncio functions such as asyncio.run(), and only rarely reference the loop object or invoke its methods directly.

The page is aimed mostly at authors of lower-level code, libraries, and frameworks who want finer control over event-loop behavior.

get_running_loop() raises RuntimeError when no loop is running, and it may be called only from a coroutine or a callback.

get_running_loop() was added in version 3.7.

From a coroutine or a callback, get_event_loop() returns the running loop. When no loop is running, it may return whatever the current policy's get_event_loop() provides.

Because that behavior is complex, especially with a custom policy, the docs prefer get_running_loop() inside coroutines and callbacks.

Since version 3.14, get_event_loop() raises RuntimeError when there is no current loop.

A custom event loop policy can change how get_event_loop(), set_event_loop(), and new_event_loop() behave.

Consider asyncio.run() instead of using lower-level functions to manually create and close an event loop.

Running and closing the loop

If the argument is a coroutine object it is implicitly scheduled to run as a Task.

run_until_complete() returns a Future's result or raises its exception.

run_forever() keeps the loop going until stop() is called.

If stop() happens while run_forever() is already running, the loop finishes the current batch of callbacks and then returns.

close() must be called when the loop is not running. Pending callbacks are discarded.

close() clears queues and shuts down the executor without waiting for that executor to finish.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, Event loop; CPython, Event loop documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, Event loop; CPython, Event loop documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:jd5qctwf

iinventedlight. 2026. "Python asyncio event loop: run tasks, callbacks, and I/O." iinventedlight Library, iil:note:jd5qctwf, version of 2026-11-26. https://iinventedlight.com/library/notes/python-asyncio-eventloop/. License: CC-BY-4.0.

@misc{iil_note_jd5qctwf,
  author = {{iinventedlight}},
  title = {{Python asyncio event loop: run tasks, callbacks, and I/O}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-26},
  url = {https://iinventedlight.com/library/notes/python-asyncio-eventloop/},
  note = {Permanent ID iil:note:jd5qctwf. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-asyncio-eventloop.md.