_subprocesses.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from __future__ import annotations
  2. from abc import abstractmethod
  3. from signal import Signals
  4. from typing import TYPE_CHECKING
  5. from ._resources import AsyncResource
  6. if TYPE_CHECKING:
  7. from ._streams import ByteReceiveStream, ByteSendStream
  8. class Process(AsyncResource):
  9. """An asynchronous version of :class:`subprocess.Popen`."""
  10. @abstractmethod
  11. async def wait(self) -> int:
  12. """
  13. Wait until the process exits.
  14. :return: the exit code of the process
  15. """
  16. @abstractmethod
  17. def terminate(self) -> None:
  18. """
  19. Terminates the process, gracefully if possible.
  20. On Windows, this calls ``TerminateProcess()``.
  21. On POSIX systems, this sends ``SIGTERM`` to the process.
  22. .. seealso:: :meth:`subprocess.Popen.terminate`
  23. """
  24. @abstractmethod
  25. def kill(self) -> None:
  26. """
  27. Kills the process.
  28. On Windows, this calls ``TerminateProcess()``.
  29. On POSIX systems, this sends ``SIGKILL`` to the process.
  30. .. seealso:: :meth:`subprocess.Popen.kill`
  31. """
  32. @abstractmethod
  33. def send_signal(self, signal: Signals) -> None:
  34. """
  35. Send a signal to the subprocess.
  36. .. seealso:: :meth:`subprocess.Popen.send_signal`
  37. :param signal: the signal number (e.g. :data:`signal.SIGHUP`)
  38. """
  39. @property
  40. @abstractmethod
  41. def pid(self) -> int:
  42. """The process ID of the process."""
  43. @property
  44. @abstractmethod
  45. def returncode(self) -> int | None:
  46. """
  47. The return code of the process. If the process has not yet terminated, this will
  48. be ``None``.
  49. """
  50. @property
  51. @abstractmethod
  52. def stdin(self) -> ByteSendStream | None:
  53. """The stream for the standard input of the process."""
  54. @property
  55. @abstractmethod
  56. def stdout(self) -> ByteReceiveStream | None:
  57. """The stream for the standard output of the process."""
  58. @property
  59. @abstractmethod
  60. def stderr(self) -> ByteReceiveStream | None:
  61. """The stream for the standard error output of the process."""