Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

88 linhas
3.0 KiB

  1. import typing
  2. from abc import ABCMeta, abstractmethod
  3. from types import TracebackType
  4. from typing import Any, Callable, Coroutine, Optional, Type, TypeVar
  5. from warnings import warn
  6. if typing.TYPE_CHECKING:
  7. from anyio._core._tasks import CancelScope
  8. T_Retval = TypeVar('T_Retval')
  9. class TaskStatus(metaclass=ABCMeta):
  10. @abstractmethod
  11. def started(self, value: object = None) -> None:
  12. """
  13. Signal that the task has started.
  14. :param value: object passed back to the starter of the task
  15. """
  16. class TaskGroup(metaclass=ABCMeta):
  17. """
  18. Groups several asynchronous tasks together.
  19. :ivar cancel_scope: the cancel scope inherited by all child tasks
  20. :vartype cancel_scope: CancelScope
  21. """
  22. cancel_scope: 'CancelScope'
  23. async def spawn(self, func: Callable[..., Coroutine[Any, Any, Any]],
  24. *args: object, name: object = None) -> None:
  25. """
  26. Start a new task in this task group.
  27. :param func: a coroutine function
  28. :param args: positional arguments to call the function with
  29. :param name: name of the task, for the purposes of introspection and debugging
  30. .. deprecated:: 3.0
  31. Use :meth:`start_soon` instead. If your code needs AnyIO 2 compatibility, you
  32. can keep using this until AnyIO 4.
  33. """
  34. warn('spawn() is deprecated -- use start_soon() (without the "await") instead',
  35. DeprecationWarning)
  36. self.start_soon(func, *args, name=name)
  37. @abstractmethod
  38. def start_soon(self, func: Callable[..., Coroutine[Any, Any, Any]],
  39. *args: object, name: object = None) -> None:
  40. """
  41. Start a new task in this task group.
  42. :param func: a coroutine function
  43. :param args: positional arguments to call the function with
  44. :param name: name of the task, for the purposes of introspection and debugging
  45. .. versionadded:: 3.0
  46. """
  47. @abstractmethod
  48. async def start(self, func: Callable[..., Coroutine[Any, Any, Any]],
  49. *args: object, name: object = None) -> object:
  50. """
  51. Start a new task and wait until it signals for readiness.
  52. :param func: a coroutine function
  53. :param args: positional arguments to call the function with
  54. :param name: name of the task, for the purposes of introspection and debugging
  55. :return: the value passed to ``task_status.started()``
  56. :raises RuntimeError: if the task finishes without calling ``task_status.started()``
  57. .. versionadded:: 3.0
  58. """
  59. @abstractmethod
  60. async def __aenter__(self) -> 'TaskGroup':
  61. """Enter the task group context and allow starting new tasks."""
  62. @abstractmethod
  63. async def __aexit__(self, exc_type: Optional[Type[BaseException]],
  64. exc_val: Optional[BaseException],
  65. exc_tb: Optional[TracebackType]) -> Optional[bool]:
  66. """Exit the task group context waiting for all tasks to finish."""