Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/anyio/to_thread.py: 80%
10 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:38 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:38 +0000
1from __future__ import annotations
3from collections.abc import Callable
4from typing import TypeVar
6from ._core._eventloop import get_async_backend
7from .abc import CapacityLimiter
9T_Retval = TypeVar("T_Retval")
12async def run_sync(
13 func: Callable[..., T_Retval],
14 *args: object,
15 cancellable: bool = False,
16 limiter: CapacityLimiter | None = None,
17) -> T_Retval:
18 """
19 Call the given function with the given arguments in a worker thread.
21 If the ``cancellable`` option is enabled and the task waiting for its completion is
22 cancelled, the thread will still run its course but its return value (or any raised
23 exception) will be ignored.
25 :param func: a callable
26 :param args: positional arguments for the callable
27 :param cancellable: ``True`` to allow cancellation of the operation
28 :param limiter: capacity limiter to use to limit the total amount of threads running
29 (if omitted, the default limiter is used)
30 :return: an awaitable that yields the return value of the function.
32 """
33 return await get_async_backend().run_sync_in_worker_thread(
34 func, args, cancellable=cancellable, limiter=limiter
35 )
38def current_default_thread_limiter() -> CapacityLimiter:
39 """
40 Return the capacity limiter that is used by default to limit the number of
41 concurrent threads.
43 :return: a capacity limiter object
45 """
46 return get_async_backend().current_default_thread_limiter()