_subprocesses.py 2.0 KB

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