MCPcopy Index your code
hub / github.com/python/cpython / to_thread

Function to_thread

Lib/asyncio/threads.py:12–25  ·  view source on GitHub ↗

Asynchronously run function *func* in a separate thread. Any *args and **kwargs supplied for this function are directly passed to *func*. Also, the current :class:`contextvars.Context` is propagated, allowing context variables from the main thread to be accessed in the separate thre

(func, /, *args, **kwargs)

Source from the content-addressed store, hash-verified

10
11
12async def to_thread(func, /, *args, **kwargs):
13 """Asynchronously run function *func* in a separate thread.
14
15 Any *args and **kwargs supplied for this function are directly passed
16 to *func*. Also, the current :class:`contextvars.Context` is propagated,
17 allowing context variables from the main thread to be accessed in the
18 separate thread.
19
20 Return a coroutine that can be awaited to get the eventual result of *func*.
21 """
22 loop = events.get_running_loop()
23 ctx = contextvars.copy_context()
24 func_call = functools.partial(ctx.run, func, *args, **kwargs)
25 return await loop.run_in_executor(None, func_call)

Callers

nothing calls this directly

Calls 2

partialMethod · 0.80
run_in_executorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…