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

16 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:19 +0000

1from __future__ import annotations 

2 

3from typing import Callable, TypeVar 

4from warnings import warn 

5 

6from ._core._eventloop import get_asynclib 

7from .abc import CapacityLimiter 

8 

9T_Retval = TypeVar("T_Retval") 

10 

11 

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. 

20 

21 If the ``cancellable`` option is enabled and the task waiting for its completion is cancelled, 

22 the thread will still run its course but its return value (or any raised exception) will be 

23 ignored. 

24 

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. 

31 

32 """ 

33 return await get_asynclib().run_sync_in_worker_thread( 

34 func, *args, cancellable=cancellable, limiter=limiter 

35 ) 

36 

37 

38async def run_sync_in_worker_thread( 

39 func: Callable[..., T_Retval], 

40 *args: object, 

41 cancellable: bool = False, 

42 limiter: CapacityLimiter | None = None, 

43) -> T_Retval: 

44 warn( 

45 "run_sync_in_worker_thread() has been deprecated, use anyio.to_thread.run_sync() instead", 

46 DeprecationWarning, 

47 ) 

48 return await run_sync(func, *args, cancellable=cancellable, limiter=limiter) 

49 

50 

51def current_default_thread_limiter() -> CapacityLimiter: 

52 """ 

53 Return the capacity limiter that is used by default to limit the number of concurrent threads. 

54 

55 :return: a capacity limiter object 

56 

57 """ 

58 return get_asynclib().current_default_thread_limiter() 

59 

60 

61def current_default_worker_thread_limiter() -> CapacityLimiter: 

62 warn( 

63 "current_default_worker_thread_limiter() has been deprecated, " 

64 "use anyio.to_thread.current_default_thread_limiter() instead", 

65 DeprecationWarning, 

66 ) 

67 return current_default_thread_limiter()