to_thread.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. __all__ = (
  3. "run_sync",
  4. "current_default_thread_limiter",
  5. )
  6. import sys
  7. from collections.abc import Callable
  8. from typing import TYPE_CHECKING, TypeVar
  9. from warnings import warn
  10. from ._core._eventloop import get_async_backend
  11. if TYPE_CHECKING:
  12. from ._core._synchronization import CapacityLimiter
  13. if sys.version_info >= (3, 11):
  14. from typing import TypeVarTuple, Unpack
  15. else:
  16. from typing_extensions import TypeVarTuple, Unpack
  17. T_Retval = TypeVar("T_Retval")
  18. PosArgsT = TypeVarTuple("PosArgsT")
  19. async def run_sync(
  20. func: Callable[[Unpack[PosArgsT]], T_Retval],
  21. *args: Unpack[PosArgsT],
  22. abandon_on_cancel: bool = False,
  23. cancellable: bool | None = None,
  24. limiter: CapacityLimiter | None = None,
  25. ) -> T_Retval:
  26. """
  27. Call the given function with the given arguments in a worker thread.
  28. If the ``abandon_on_cancel`` option is enabled and the task waiting for its
  29. completion is cancelled, the thread will still run its course but its
  30. return value (or any raised exception) will be ignored.
  31. :param func: a callable
  32. :param args: positional arguments for the callable
  33. :param abandon_on_cancel: ``True`` to abandon the thread (leaving it to run
  34. unchecked on own) if the host task is cancelled, ``False`` to ignore
  35. cancellations in the host task until the operation has completed in the worker
  36. thread
  37. :param cancellable: deprecated alias of ``abandon_on_cancel``; will override
  38. ``abandon_on_cancel`` if both parameters are passed
  39. :param limiter: capacity limiter to use to limit the total amount of threads running
  40. (if omitted, the default limiter is used)
  41. :raises NoEventLoopError: if no supported asynchronous event loop is running in the
  42. current thread
  43. :return: an awaitable that yields the return value of the function.
  44. """
  45. if cancellable is not None:
  46. abandon_on_cancel = cancellable
  47. warn(
  48. "The `cancellable=` keyword argument to `anyio.to_thread.run_sync` is "
  49. "deprecated since AnyIO 4.1.0; use `abandon_on_cancel=` instead",
  50. DeprecationWarning,
  51. stacklevel=2,
  52. )
  53. return await get_async_backend().run_sync_in_worker_thread(
  54. func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
  55. )
  56. def current_default_thread_limiter() -> CapacityLimiter:
  57. """
  58. Return the capacity limiter that is used by default to limit the number of
  59. concurrent threads.
  60. :return: a capacity limiter object
  61. :raises NoEventLoopError: if no supported asynchronous event loop is running in the
  62. current thread
  63. """
  64. return get_async_backend().current_default_thread_limiter()