Python Glossary Русский

Python 3.7+Async programmingfunction

asyncio.get_running_loop()

event loop

Returns the event loop running in the current OS thread; call it from coroutines and callbacks.

Syntax

loop = asyncio.get_running_loop()

The preferred replacement for get_event_loop() inside async code: its behaviour is unambiguous, no loop means RuntimeError, and nothing is created implicitly. Calling it from plain code outside asyncio.run() is pointless. You need the loop for low-level calls: loop.time(), call_later, run_in_executor, create_future. Added in 3.7.

Examples

import asyncio
async def main():
    loop = asyncio.get_running_loop()
    loop.call_later(0.01, print, 'через 10 мс')
    await asyncio.sleep(0.02)
    print(loop.is_running())  # → True
asyncio.run(main())  # → через 10 мс / True
import asyncio
asyncio.get_running_loop()  # → RuntimeError: no running event loop

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy