Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/anyio/to_thread.py: 67%

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

21 statements  

1from __future__ import annotations 

2 

3__all__ = ( 

4 "run_sync", 

5 "current_default_thread_limiter", 

6) 

7 

8import sys 

9from collections.abc import Callable 

10from typing import TYPE_CHECKING, TypeVar 

11from warnings import warn 

12 

13from ._core._eventloop import get_async_backend 

14 

15if TYPE_CHECKING: 

16 from ._core._synchronization import CapacityLimiter 

17 

18if sys.version_info >= (3, 11): 

19 from typing import TypeVarTuple, Unpack 

20else: 

21 from typing_extensions import TypeVarTuple, Unpack 

22 

23T_Retval = TypeVar("T_Retval") 

24PosArgsT = TypeVarTuple("PosArgsT") 

25 

26 

27async def run_sync( 

28 func: Callable[[Unpack[PosArgsT]], T_Retval], 

29 *args: Unpack[PosArgsT], 

30 abandon_on_cancel: bool = False, 

31 cancellable: bool | None = None, 

32 limiter: CapacityLimiter | None = None, 

33) -> T_Retval: 

34 """ 

35 Call the given function with the given arguments in a worker thread. 

36 

37 If the ``abandon_on_cancel`` option is enabled and the task waiting for its 

38 completion is cancelled, the thread will still run its course but its 

39 return value (or any raised exception) will be ignored. 

40 

41 :param func: a callable 

42 :param args: positional arguments for the callable 

43 :param abandon_on_cancel: ``True`` to abandon the thread (leaving it to run 

44 unchecked on own) if the host task is cancelled, ``False`` to ignore 

45 cancellations in the host task until the operation has completed in the worker 

46 thread 

47 :param cancellable: deprecated alias of ``abandon_on_cancel``; will override 

48 ``abandon_on_cancel`` if both parameters are passed 

49 :param limiter: capacity limiter to use to limit the total amount of threads running 

50 (if omitted, the default limiter is used) 

51 :raises NoEventLoopError: if no supported asynchronous event loop is running in the 

52 current thread 

53 :return: an awaitable that yields the return value of the function. 

54 

55 """ 

56 if cancellable is not None: 

57 abandon_on_cancel = cancellable 

58 warn( 

59 "The `cancellable=` keyword argument to `anyio.to_thread.run_sync` is " 

60 "deprecated since AnyIO 4.1.0; use `abandon_on_cancel=` instead", 

61 DeprecationWarning, 

62 stacklevel=2, 

63 ) 

64 

65 return await get_async_backend().run_sync_in_worker_thread( 

66 func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter 

67 ) 

68 

69 

70def current_default_thread_limiter() -> CapacityLimiter: 

71 """ 

72 Return the capacity limiter that is used by default to limit the number of 

73 concurrent threads. 

74 

75 :return: a capacity limiter object 

76 :raises NoEventLoopError: if no supported asynchronous event loop is running in the 

77 current thread 

78 

79 """ 

80 return get_async_backend().current_default_thread_limiter()