| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | Cross-platform process-related functionality used by different |
|---|
| 7 | L{IReactorProcess} implementations. |
|---|
| 8 | """ |
|---|
| 9 | |
|---|
| 10 | from twisted.python.reflect import qual |
|---|
| 11 | from twisted.python.deprecate import getWarningMethod |
|---|
| 12 | from twisted.python.failure import Failure |
|---|
| 13 | from twisted.python.log import err |
|---|
| 14 | from twisted.persisted.styles import Ephemeral |
|---|
| 15 | |
|---|
| 16 | _missingProcessExited = ("Since Twisted 8.2, IProcessProtocol.processExited " |
|---|
| 17 | "is required. %s must implement it.") |
|---|
| 18 | |
|---|
| 19 | class BaseProcess(Ephemeral): |
|---|
| 20 | pid = None |
|---|
| 21 | status = None |
|---|
| 22 | lostProcess = 0 |
|---|
| 23 | proto = None |
|---|
| 24 | |
|---|
| 25 | def __init__(self, protocol): |
|---|
| 26 | self.proto = protocol |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | def _callProcessExited(self, reason): |
|---|
| 30 | default = object() |
|---|
| 31 | processExited = getattr(self.proto, 'processExited', default) |
|---|
| 32 | if processExited is default: |
|---|
| 33 | getWarningMethod()( |
|---|
| 34 | _missingProcessExited % (qual(self.proto.__class__),), |
|---|
| 35 | DeprecationWarning, stacklevel=0) |
|---|
| 36 | else: |
|---|
| 37 | processExited(Failure(reason)) |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | def processEnded(self, status): |
|---|
| 41 | """ |
|---|
| 42 | This is called when the child terminates. |
|---|
| 43 | """ |
|---|
| 44 | self.status = status |
|---|
| 45 | self.lostProcess += 1 |
|---|
| 46 | self.pid = None |
|---|
| 47 | self._callProcessExited(self._getReason(status)) |
|---|
| 48 | self.maybeCallProcessEnded() |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | def maybeCallProcessEnded(self): |
|---|
| 52 | """ |
|---|
| 53 | Call processEnded on protocol after final cleanup. |
|---|
| 54 | """ |
|---|
| 55 | if self.proto is not None: |
|---|
| 56 | reason = self._getReason(self.status) |
|---|
| 57 | proto = self.proto |
|---|
| 58 | self.proto = None |
|---|
| 59 | try: |
|---|
| 60 | proto.processEnded(Failure(reason)) |
|---|
| 61 | except: |
|---|
| 62 | err(None, "unexpected error in processEnded") |
|---|