Python Glossary Русский

Python 3.12+Async programmingfunction

asyncio.create_eager_task_factory()

tasks

Builds an eager task factory around a custom task class, for example a Task subclass that adds logging.

Syntax

loop.set_task_factory(asyncio.create_eager_task_factory(custom_task_constructor))

eager_task_factory is exactly create_eager_task_factory(asyncio.Task); you need your own factory only to swap the task class. The class must accept the eager_start argument like asyncio.Task does: a plain subclass that leaves __init__ alone works. Eager start changes execution order just as the stock factory does. Added in 3.12.

Examples

import asyncio
class MyTask(asyncio.Task):
    pass
async def quick():
    return 'готово'
async def main():
    loop = asyncio.get_running_loop()
    loop.set_task_factory(asyncio.create_eager_task_factory(MyTask))
    task = asyncio.create_task(quick())
    print(type(task).__name__, task.done())  # → MyTask True
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy