Wrapper around `asyncio.create_task`. - Use `keep_ref` to keep an internal reference. This ensures that the task is not garbage collected mid-execution if no other reference is kept. - Use `client` to pass the client address as additional debug info on the task.
(
coro: Coroutine,
*,
name: str,
keep_ref: bool,
client: tuple | None = None,
)
| 12 | |
| 13 | |
| 14 | def create_task( |
| 15 | coro: Coroutine, |
| 16 | *, |
| 17 | name: str, |
| 18 | keep_ref: bool, |
| 19 | client: tuple | None = None, |
| 20 | ) -> asyncio.Task: |
| 21 | """ |
| 22 | Wrapper around `asyncio.create_task`. |
| 23 | |
| 24 | - Use `keep_ref` to keep an internal reference. |
| 25 | This ensures that the task is not garbage collected mid-execution if no other reference is kept. |
| 26 | - Use `client` to pass the client address as additional debug info on the task. |
| 27 | """ |
| 28 | t = asyncio.create_task(coro) # noqa: TID251 |
| 29 | set_task_debug_info(t, name=name, client=client) |
| 30 | if keep_ref and not t.done(): |
| 31 | # The event loop only keeps weak references to tasks. |
| 32 | # A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. |
| 33 | _KEEP_ALIVE.add(t) |
| 34 | t.add_done_callback(_KEEP_ALIVE.discard) |
| 35 | return t |
| 36 | |
| 37 | |
| 38 | def set_task_debug_info( |
nothing calls this directly
no test coverage detected
searching dependent graphs…