A subprocess worker via Popen. PopenWorker provides a low-level API to interact with a separate process via Popen. Parameters ---------- initializer: callable or None A callable initializer, or None initargs: Tuple[object] A tuple of args for the initialize
| 82 | |
| 83 | |
| 84 | class PopenWorker: |
| 85 | """A subprocess worker via Popen. |
| 86 | |
| 87 | PopenWorker provides a low-level |
| 88 | API to interact with a separate process via Popen. |
| 89 | |
| 90 | Parameters |
| 91 | ---------- |
| 92 | initializer: callable or None |
| 93 | A callable initializer, or None |
| 94 | |
| 95 | initargs: Tuple[object] |
| 96 | A tuple of args for the initializer |
| 97 | |
| 98 | maximum_uses: Optional[int] |
| 99 | The maximum number of times a process can be used before being recycled, |
| 100 | i.e. killed and restarted. If `None`, the process will be reused until |
| 101 | an operation times out. |
| 102 | |
| 103 | stdout: Union[None, int, IO[Any]] |
| 104 | The standard output streams handler specified for the popen process. |
| 105 | |
| 106 | stderr: Union[None, int, IO[Any]] |
| 107 | The standard error streams handler specified for the popen process. |
| 108 | """ |
| 109 | |
| 110 | def __init__(self, initializer=None, initargs=(), maximum_uses=None, stdout=None, stderr=None): |
| 111 | self._proc = None |
| 112 | self._initializer = initializer |
| 113 | self._initargs = initargs |
| 114 | self._maximum_uses = maximum_uses |
| 115 | self._remaining_uses = None |
| 116 | self._stdout = stdout |
| 117 | self._stderr = stderr |
| 118 | |
| 119 | if self._initializer is not None and not callable(self._initializer): |
| 120 | raise TypeError("initializer must be callable for PopenWorker") |
| 121 | |
| 122 | def __del__(self): |
| 123 | try: |
| 124 | self.kill() |
| 125 | except ImportError: |
| 126 | pass |
| 127 | |
| 128 | def kill(self): |
| 129 | """Kill the current running process and cleanup. |
| 130 | |
| 131 | Note |
| 132 | ---- |
| 133 | The worker can start a new process when send is called again. |
| 134 | """ |
| 135 | if self._proc is not None: |
| 136 | # allow gracefully shutdown |
| 137 | try: |
| 138 | self._writer.close() |
| 139 | except OSError: |
| 140 | pass |
| 141 | try: |
no outgoing calls
searching dependent graphs…