The main direction for
asyncio extending is writing custom event loop
classes. Asyncio has helpers that could be used to simplify this task.
Third-parties should reuse existing asyncio code with caution, a new Python version is free to break backward compatibility in internal part of API.
Writing a Custom Event Loop¶
asyncio.AbstractEventLoop declares very many methods. Implementing all them
from scratch is a tedious job.
A loop can get many common methods implementation for free by inheriting from
In turn, the successor should implement a bunch of private methods declared but not
loop.create_connection() checks arguments, resolves DNS addresses, and
loop._make_socket_transport() that should be implemented by inherited class.
_make_socket_transport() method is not documented and is considered as an
Future and Task private constructors¶
However, third-party event loops may reuse built-in future and task implementations for the sake of getting a complex and highly optimized code for free.
For this purpose the following, private constructors are listed:
- Future.__init__(*, loop=None)¶
Create a built-in future instance.
loop is an optional event loop instance.
- Task.__init__(coro, *, loop=None, name=None, context=None)¶
Create a built-in task instance.
loop is an optional event loop instance. The rest of arguments are described in
Changed in version 3.11: context argument is added.
Trueif future is attached to the same loop as the task,
New in version 3.11.
Task lifetime support¶
A third party task implementation should call the following functions to keep a task
Register a new task as managed by asyncio.
Call the function from a task constructor.
Unregister a task from asyncio internal structures.
The function should be called when a task is about to finish.
- asyncio._enter_task(loop, task)¶
Switch the current task to the task argument.