Python Glossary Русский

Python 3.12+Async programmingfunction

asyncio.eager_task_factory()

tasks

A ready-made eager task factory: the coroutine starts running inside create_task, up to its first await, instead of on the next loop iteration.

Syntax

loop.set_task_factory(asyncio.eager_task_factory)

Enable it with loop.set_task_factory(asyncio.eager_task_factory). A coroutine that finishes without suspending yields a task that is done immediately and never touches the scheduler, a real speed-up for cache-heavy code. But ordering changes: code before the first await runs before the line after create_task, and programs relying on the old order break. Added in 3.12.

Examples

import asyncio
async def cached():
    print('выполняется')
    return 42
async def main():
    asyncio.get_running_loop().set_task_factory(asyncio.eager_task_factory)
    task = asyncio.create_task(cached())  # → выполняется — ещё внутри create_task
    print(task.done(), task.result())  # → True 42
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy