Python Glossary Русский

Python 3.4+Async programmingterm

asyncio.AbstractEventLoop

event loop

The abstract base class of asyncio event loops: it lists what every loop can do — schedule callbacks, watch sockets, run subprocesses.

Syntax

loop = asyncio.get_running_loop()  # isinstance(loop, asyncio.AbstractEventLoop)

Learners almost never need it: asyncio.run() starts the whole program, asyncio.create_task() schedules work, and the loop stays behind the scenes. You fetch the loop object only for rare methods — loop.run_in_executor() to push a blocking call into a thread, or loop.time() — and you get it with asyncio.get_running_loop() inside a coroutine, not asyncio.get_event_loop(), whose behaviour outside a loop differs between versions. Subclassing it only makes sense when you write a loop implementation such as uvloop.

Examples

import asyncio
import time
def blocking():
    time.sleep(0.01)  # обычная блокирующая функция
    return 'готово'
async def main():
    loop = asyncio.get_running_loop()
    print(isinstance(loop, asyncio.AbstractEventLoop))  # → True
    result = await loop.run_in_executor(None, blocking)  # в пуле потоков, цикл не стоит
    print(result)  # → готово
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy