logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

threads.py (790B)


  1. """High-level support for working with threads in asyncio"""
  2. import functools
  3. import contextvars
  4. from . import events
  5. __all__ = "to_thread",
  6. async def to_thread(func, /, *args, **kwargs):
  7. """Asynchronously run function *func* in a separate thread.
  8. Any *args and **kwargs supplied for this function are directly passed
  9. to *func*. Also, the current :class:`contextvars.Context` is propagated,
  10. allowing context variables from the main thread to be accessed in the
  11. separate thread.
  12. Return a coroutine that can be awaited to get the eventual result of *func*.
  13. """
  14. loop = events.get_running_loop()
  15. ctx = contextvars.copy_context()
  16. func_call = functools.partial(ctx.run, func, *args, **kwargs)
  17. return await loop.run_in_executor(None, func_call)