MCPcopy Index your code
hub / github.com/mitmproxy/mitmproxy / create_task

Function create_task

mitmproxy/utils/asyncio_utils.py:14–35  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

12
13
14def 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
38def set_task_debug_info(

Callers

nothing calls this directly

Calls 3

set_task_debug_infoFunction · 0.85
doneMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…