Current Dir: /snap/core22/2437/usr/lib/python3.10/asyncio/

[DIR]  __pycache__ [ delete | rename ]
[FILE] base_events.py [ edit | delete | rename ]
[FILE] base_futures.py [ edit | delete | rename ]
[FILE] base_subprocess.py [ edit | delete | rename ]
[FILE] base_tasks.py [ edit | delete | rename ]
[FILE] constants.py [ edit | delete | rename ]
[FILE] coroutines.py [ edit | delete | rename ]
[FILE] events.py [ edit | delete | rename ]
[FILE] exceptions.py [ edit | delete | rename ]
[FILE] format_helpers.py [ edit | delete | rename ]
[FILE] futures.py [ edit | delete | rename ]
[FILE] locks.py [ edit | delete | rename ]
[FILE] log.py [ edit | delete | rename ]
[FILE] mixins.py [ edit | delete | rename ]
[FILE] proactor_events.py [ edit | delete | rename ]
[FILE] protocols.py [ edit | delete | rename ]
[FILE] queues.py [ edit | delete | rename ]
[FILE] runners.py [ edit | delete | rename ]
[FILE] selector_events.py [ edit | delete | rename ]
[FILE] sslproto.py [ edit | delete | rename ]
[FILE] staggered.py [ edit | delete | rename ]
[FILE] streams.py [ edit | delete | rename ]
[FILE] subprocess.py [ edit | delete | rename ]
[FILE] tasks.py [ edit | delete | rename ]
[FILE] threads.py [ edit | delete | rename ]
[FILE] transports.py [ edit | delete | rename ]
[FILE] trsock.py [ edit | delete | rename ]
[FILE] unix_events.py [ edit | delete | rename ]
[FILE] windows_events.py [ edit | delete | rename ]
[FILE] windows_utils.py [ edit | delete | rename ]
[FILE] __init__.py [ edit | delete | rename ]
[FILE] __main__.py [ edit | delete | rename ]

Viewing: /snap/core22/2437/usr/lib/python3.10/asyncio/threads.py

"""High-level support for working with threads in asyncio"""

import functools
import contextvars

from . import events


__all__ = "to_thread",


async def to_thread(func, /, *args, **kwargs):
    """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 thread.

    Return a coroutine that can be awaited to get the eventual result of *func*.
    """
    loop = events.get_running_loop()
    ctx = contextvars.copy_context()
    func_call = functools.partial(ctx.run, func, *args, **kwargs)
    return await loop.run_in_executor(None, func_call)

Upload File: