Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

55 rindas
2.1 KiB

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