Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/anyio/to_thread.py: 68%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import sys
4from collections.abc import Callable
5from typing import TypeVar
6from warnings import warn
8from ._core._eventloop import get_async_backend
9from .abc import CapacityLimiter
11if sys.version_info >= (3, 11):
12 from typing import TypeVarTuple, Unpack
13else:
14 from typing_extensions import TypeVarTuple, Unpack
16T_Retval = TypeVar("T_Retval")
17PosArgsT = TypeVarTuple("PosArgsT")
20async def run_sync(
21 func: Callable[[Unpack[PosArgsT]], T_Retval],
22 *args: Unpack[PosArgsT],
23 abandon_on_cancel: bool = False,
24 cancellable: bool | None = None,
25 limiter: CapacityLimiter | None = None,
26) -> T_Retval:
27 """
28 Call the given function with the given arguments in a worker thread.
30 If the ``cancellable`` option is enabled and the task waiting for its completion is
31 cancelled, the thread will still run its course but its return value (or any raised
32 exception) will be ignored.
34 :param func: a callable
35 :param args: positional arguments for the callable
36 :param abandon_on_cancel: ``True`` to abandon the thread (leaving it to run
37 unchecked on own) if the host task is cancelled, ``False`` to ignore
38 cancellations in the host task until the operation has completed in the worker
39 thread
40 :param cancellable: deprecated alias of ``abandon_on_cancel``; will override
41 ``abandon_on_cancel`` if both parameters are passed
42 :param limiter: capacity limiter to use to limit the total amount of threads running
43 (if omitted, the default limiter is used)
44 :return: an awaitable that yields the return value of the function.
46 """
47 if cancellable is not None:
48 abandon_on_cancel = cancellable
49 warn(
50 "The `cancellable=` keyword argument to `anyio.to_thread.run_sync` is "
51 "deprecated since AnyIO 4.1.0; use `abandon_on_cancel=` instead",
52 DeprecationWarning,
53 stacklevel=2,
54 )
56 return await get_async_backend().run_sync_in_worker_thread(
57 func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
58 )
61def current_default_thread_limiter() -> CapacityLimiter:
62 """
63 Return the capacity limiter that is used by default to limit the number of
64 concurrent threads.
66 :return: a capacity limiter object
68 """
69 return get_async_backend().current_default_thread_limiter()