1from typing import Callable, Optional, TypeVar
2from warnings import warn
3
4from ._core._eventloop import get_asynclib
5from .abc import CapacityLimiter
6
7T_Retval = TypeVar('T_Retval')
8
9
10async def run_sync(
11        func: Callable[..., T_Retval], *args: object, cancellable: bool = False,
12        limiter: Optional[CapacityLimiter] = None) -> T_Retval:
13    """
14    Call the given function with the given arguments in a worker thread.
15
16    If the ``cancellable`` option is enabled and the task waiting for its completion is cancelled,
17    the thread will still run its course but its return value (or any raised exception) will be
18    ignored.
19
20    :param func: a callable
21    :param args: positional arguments for the callable
22    :param cancellable: ``True`` to allow cancellation of the operation
23    :param limiter: capacity limiter to use to limit the total amount of threads running
24        (if omitted, the default limiter is used)
25    :return: an awaitable that yields the return value of the function.
26
27    """
28    return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
29                                                          limiter=limiter)
30
31
32async def run_sync_in_worker_thread(
33        func: Callable[..., T_Retval], *args: object, cancellable: bool = False,
34        limiter: Optional[CapacityLimiter] = None) -> T_Retval:
35    warn('run_sync_in_worker_thread() has been deprecated, use anyio.to_thread.run_sync() instead',
36         DeprecationWarning)
37    return await run_sync(func, *args, cancellable=cancellable, limiter=limiter)
38
39
40def current_default_thread_limiter() -> CapacityLimiter:
41    """
42    Return the capacity limiter that is used by default to limit the number of concurrent threads.
43
44    :return: a capacity limiter object
45
46    """
47    return get_asynclib().current_default_thread_limiter()
48
49
50def current_default_worker_thread_limiter() -> CapacityLimiter:
51    warn('current_default_worker_thread_limiter() has been deprecated, '
52         'use anyio.to_thread.current_default_thread_limiter() instead',
53         DeprecationWarning)
54    return current_default_thread_limiter()
55