Python Glossary Русский

Python 3.4+Async programmingfunction

asyncio.get_event_loop()

event loop

Returns the current event loop: the running one inside a coroutine, otherwise the one set for the thread. In async code prefer get_running_loop().

Syntax

loop = asyncio.get_event_loop()

The most volatile function in asyncio. Before 3.10 it silently created a loop when none existed; in 3.10-3.13 that creation came with a DeprecationWarning, and since 3.14 it raises RuntimeError when no loop is set. So old recipes like loop = asyncio.get_event_loop(); loop.run_until_complete(...) no longer work; asyncio.run() replaces them. Inside a coroutine it equals get_running_loop().

Examples

import asyncio
async def main():
    print(asyncio.get_event_loop() is asyncio.get_running_loop())  # → True
asyncio.run(main())  # вместо get_event_loop().run_until_complete(main())

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy