Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | # |
| 2 | # Module providing the `Process` class which emulates `threading.Thread` |
| 3 | # |
| 4 | # multiprocessing/process.py |
| 5 | # |
R. David Murray | 3fc969a | 2010-12-14 01:38:16 +0000 | [diff] [blame] | 6 | # Copyright (c) 2006-2008, R Oudkerk |
Richard Oudkerk | 3e268aa | 2012-04-30 12:13:55 +0100 | [diff] [blame] | 7 | # Licensed to PSF under a Contributor Agreement. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 8 | # |
| 9 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 10 | __all__ = ['BaseProcess', 'current_process', 'active_children'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 11 | |
| 12 | # |
| 13 | # Imports |
| 14 | # |
| 15 | |
| 16 | import os |
| 17 | import sys |
| 18 | import signal |
| 19 | import itertools |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 20 | from _weakrefset import WeakSet |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 21 | |
| 22 | # |
| 23 | # |
| 24 | # |
| 25 | |
| 26 | try: |
| 27 | ORIGINAL_DIR = os.path.abspath(os.getcwd()) |
| 28 | except OSError: |
| 29 | ORIGINAL_DIR = None |
| 30 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 31 | # |
| 32 | # Public functions |
| 33 | # |
| 34 | |
| 35 | def current_process(): |
| 36 | ''' |
| 37 | Return process object representing the current process |
| 38 | ''' |
| 39 | return _current_process |
| 40 | |
| 41 | def active_children(): |
| 42 | ''' |
| 43 | Return list of process objects corresponding to live child processes |
| 44 | ''' |
| 45 | _cleanup() |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 46 | return list(_children) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 47 | |
| 48 | # |
| 49 | # |
| 50 | # |
| 51 | |
| 52 | def _cleanup(): |
| 53 | # check for processes which have finished |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 54 | for p in list(_children): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 55 | if p._popen.poll() is not None: |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 56 | _children.discard(p) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 57 | |
| 58 | # |
| 59 | # The `Process` class |
| 60 | # |
| 61 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 62 | class BaseProcess(object): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 63 | ''' |
| 64 | Process objects represent activity that is run in a separate process |
| 65 | |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 66 | The class is analogous to `threading.Thread` |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 67 | ''' |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 68 | def _Popen(self): |
| 69 | raise NotImplementedError |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | 0bd4deb | 2011-02-25 22:07:43 +0000 | [diff] [blame] | 71 | def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, |
| 72 | *, daemon=None): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 73 | assert group is None, 'group argument must be None for now' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 74 | count = next(_process_counter) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 75 | self._identity = _current_process._identity + (count,) |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 76 | self._config = _current_process._config.copy() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 77 | self._parent_pid = os.getpid() |
| 78 | self._popen = None |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 79 | self._closed = False |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 80 | self._target = target |
| 81 | self._args = tuple(args) |
| 82 | self._kwargs = dict(kwargs) |
| 83 | self._name = name or type(self).__name__ + '-' + \ |
| 84 | ':'.join(str(i) for i in self._identity) |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 85 | if daemon is not None: |
| 86 | self.daemon = daemon |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 87 | _dangling.add(self) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 88 | |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 89 | def _check_closed(self): |
| 90 | if self._closed: |
| 91 | raise ValueError("process object is closed") |
| 92 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 93 | def run(self): |
| 94 | ''' |
| 95 | Method to be run in sub-process; can be overridden in sub-class |
| 96 | ''' |
| 97 | if self._target: |
| 98 | self._target(*self._args, **self._kwargs) |
| 99 | |
| 100 | def start(self): |
| 101 | ''' |
| 102 | Start child process |
| 103 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 104 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 105 | assert self._popen is None, 'cannot start a process twice' |
| 106 | assert self._parent_pid == os.getpid(), \ |
| 107 | 'can only start a process object created by current process' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 108 | assert not _current_process._config.get('daemon'), \ |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 109 | 'daemonic processes are not allowed to have children' |
| 110 | _cleanup() |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 111 | self._popen = self._Popen(self) |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 112 | self._sentinel = self._popen.sentinel |
Antoine Pitrou | 79d37ae | 2017-06-28 12:29:08 +0200 | [diff] [blame] | 113 | # Avoid a refcycle if the target function holds an indirect |
| 114 | # reference to the process object (see bpo-30775) |
| 115 | del self._target, self._args, self._kwargs |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 116 | _children.add(self) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 117 | |
| 118 | def terminate(self): |
| 119 | ''' |
| 120 | Terminate process; sends SIGTERM signal or uses TerminateProcess() |
| 121 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 122 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 123 | self._popen.terminate() |
| 124 | |
Vitor Pereira | ba75af7 | 2017-07-18 16:34:23 +0100 | [diff] [blame^] | 125 | def kill(self): |
| 126 | ''' |
| 127 | Terminate process; sends SIGKILL signal or uses TerminateProcess() |
| 128 | ''' |
| 129 | self._check_closed() |
| 130 | self._popen.kill() |
| 131 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 132 | def join(self, timeout=None): |
| 133 | ''' |
| 134 | Wait until child process terminates |
| 135 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 136 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 137 | assert self._parent_pid == os.getpid(), 'can only join a child process' |
| 138 | assert self._popen is not None, 'can only join a started process' |
| 139 | res = self._popen.wait(timeout) |
| 140 | if res is not None: |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 141 | _children.discard(self) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 142 | |
| 143 | def is_alive(self): |
| 144 | ''' |
| 145 | Return whether process is alive |
| 146 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 147 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 148 | if self is _current_process: |
| 149 | return True |
| 150 | assert self._parent_pid == os.getpid(), 'can only test a child process' |
| 151 | if self._popen is None: |
| 152 | return False |
| 153 | self._popen.poll() |
| 154 | return self._popen.returncode is None |
| 155 | |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 156 | def close(self): |
| 157 | ''' |
| 158 | Close the Process object. |
| 159 | |
| 160 | This method releases resources held by the Process object. It is |
| 161 | an error to call this method if the child process is still running. |
| 162 | ''' |
| 163 | if self._popen is not None: |
| 164 | if self._popen.poll() is None: |
| 165 | raise ValueError("Cannot close a process while it is still running. " |
| 166 | "You should first call join() or terminate().") |
| 167 | self._popen.close() |
| 168 | self._popen = None |
| 169 | del self._sentinel |
| 170 | _children.discard(self) |
| 171 | self._closed = True |
| 172 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 173 | @property |
| 174 | def name(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 175 | return self._name |
| 176 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 177 | @name.setter |
| 178 | def name(self, name): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 179 | assert isinstance(name, str), 'name must be a string' |
| 180 | self._name = name |
| 181 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 182 | @property |
| 183 | def daemon(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 184 | ''' |
| 185 | Return whether process is a daemon |
| 186 | ''' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 187 | return self._config.get('daemon', False) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 188 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 189 | @daemon.setter |
| 190 | def daemon(self, daemonic): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 191 | ''' |
| 192 | Set whether process is a daemon |
| 193 | ''' |
| 194 | assert self._popen is None, 'process has already started' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 195 | self._config['daemon'] = daemonic |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 196 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 197 | @property |
| 198 | def authkey(self): |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 199 | return self._config['authkey'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 200 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 201 | @authkey.setter |
| 202 | def authkey(self, authkey): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 203 | ''' |
| 204 | Set authorization key of process |
| 205 | ''' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 206 | self._config['authkey'] = AuthenticationString(authkey) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 207 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 208 | @property |
| 209 | def exitcode(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 210 | ''' |
| 211 | Return exit code of process or `None` if it has yet to stop |
| 212 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 213 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 214 | if self._popen is None: |
| 215 | return self._popen |
| 216 | return self._popen.poll() |
| 217 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 218 | @property |
| 219 | def ident(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 220 | ''' |
Florent Xicluna | b519d23 | 2010-03-04 16:10:55 +0000 | [diff] [blame] | 221 | Return identifier (PID) of process or `None` if it has yet to start |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 222 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 223 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 224 | if self is _current_process: |
| 225 | return os.getpid() |
| 226 | else: |
| 227 | return self._popen and self._popen.pid |
| 228 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 229 | pid = ident |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 230 | |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 231 | @property |
| 232 | def sentinel(self): |
| 233 | ''' |
| 234 | Return a file descriptor (Unix) or handle (Windows) suitable for |
| 235 | waiting for process termination. |
| 236 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 237 | self._check_closed() |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 238 | try: |
| 239 | return self._sentinel |
| 240 | except AttributeError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 241 | raise ValueError("process not started") from None |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 242 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 243 | def __repr__(self): |
| 244 | if self is _current_process: |
| 245 | status = 'started' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 246 | elif self._closed: |
| 247 | status = 'closed' |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 248 | elif self._parent_pid != os.getpid(): |
| 249 | status = 'unknown' |
| 250 | elif self._popen is None: |
| 251 | status = 'initial' |
| 252 | else: |
| 253 | if self._popen.poll() is not None: |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 254 | status = self.exitcode |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 255 | else: |
| 256 | status = 'started' |
| 257 | |
| 258 | if type(status) is int: |
| 259 | if status == 0: |
| 260 | status = 'stopped' |
| 261 | else: |
| 262 | status = 'stopped[%s]' % _exitcode_to_name.get(status, status) |
| 263 | |
| 264 | return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 265 | status, self.daemon and ' daemon' or '') |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 266 | |
| 267 | ## |
| 268 | |
| 269 | def _bootstrap(self): |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 270 | from . import util, context |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 271 | global _current_process, _process_counter, _children |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 272 | |
| 273 | try: |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 274 | if self._start_method is not None: |
| 275 | context._force_start_method(self._start_method) |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 276 | _process_counter = itertools.count(1) |
| 277 | _children = set() |
Victor Stinner | a6d865c | 2016-03-25 09:29:50 +0100 | [diff] [blame] | 278 | util._close_stdin() |
Victor Stinner | 0f83b15 | 2011-06-17 12:31:49 +0200 | [diff] [blame] | 279 | old_process = _current_process |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 280 | _current_process = self |
Victor Stinner | 0f83b15 | 2011-06-17 12:31:49 +0200 | [diff] [blame] | 281 | try: |
| 282 | util._finalizer_registry.clear() |
| 283 | util._run_after_forkers() |
| 284 | finally: |
| 285 | # delay finalization of the old process object until after |
| 286 | # _run_after_forkers() is executed |
| 287 | del old_process |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 288 | util.info('child process calling self.run()') |
| 289 | try: |
| 290 | self.run() |
| 291 | exitcode = 0 |
| 292 | finally: |
| 293 | util._exit_function() |
| 294 | except SystemExit as e: |
| 295 | if not e.args: |
| 296 | exitcode = 1 |
Richard Oudkerk | 29471de | 2012-06-06 19:04:57 +0100 | [diff] [blame] | 297 | elif isinstance(e.args[0], int): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 298 | exitcode = e.args[0] |
| 299 | else: |
Richard Oudkerk | 29471de | 2012-06-06 19:04:57 +0100 | [diff] [blame] | 300 | sys.stderr.write(str(e.args[0]) + '\n') |
Richard Oudkerk | 8731d7b | 2013-11-17 17:24:11 +0000 | [diff] [blame] | 301 | exitcode = 1 |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 302 | except: |
| 303 | exitcode = 1 |
| 304 | import traceback |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 305 | sys.stderr.write('Process %s:\n' % self.name) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 306 | traceback.print_exc() |
Antoine Pitrou | 84a0fbf | 2012-01-27 10:52:37 +0100 | [diff] [blame] | 307 | finally: |
| 308 | util.info('process exiting with exitcode %d' % exitcode) |
| 309 | sys.stdout.flush() |
| 310 | sys.stderr.flush() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 311 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 312 | return exitcode |
| 313 | |
| 314 | # |
| 315 | # We subclass bytes to avoid accidental transmission of auth keys over network |
| 316 | # |
| 317 | |
| 318 | class AuthenticationString(bytes): |
| 319 | def __reduce__(self): |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 320 | from .context import get_spawning_popen |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 321 | if get_spawning_popen() is None: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 322 | raise TypeError( |
| 323 | 'Pickling an AuthenticationString object is ' |
| 324 | 'disallowed for security reasons' |
| 325 | ) |
| 326 | return AuthenticationString, (bytes(self),) |
| 327 | |
| 328 | # |
| 329 | # Create object representing the main process |
| 330 | # |
| 331 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 332 | class _MainProcess(BaseProcess): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 333 | |
| 334 | def __init__(self): |
| 335 | self._identity = () |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 336 | self._name = 'MainProcess' |
| 337 | self._parent_pid = None |
| 338 | self._popen = None |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 339 | self._closed = False |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 340 | self._config = {'authkey': AuthenticationString(os.urandom(32)), |
Richard Oudkerk | e943697 | 2013-11-02 17:05:07 +0000 | [diff] [blame] | 341 | 'semprefix': '/mp'} |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 342 | # Note that some versions of FreeBSD only allow named |
Richard Oudkerk | e943697 | 2013-11-02 17:05:07 +0000 | [diff] [blame] | 343 | # semaphores to have names of up to 14 characters. Therefore |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 344 | # we choose a short prefix. |
Richard Oudkerk | e943697 | 2013-11-02 17:05:07 +0000 | [diff] [blame] | 345 | # |
| 346 | # On MacOSX in a sandbox it may be necessary to use a |
| 347 | # different prefix -- see #19478. |
| 348 | # |
| 349 | # Everything in self._config will be inherited by descendant |
| 350 | # processes. |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 351 | |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 352 | def close(self): |
| 353 | pass |
| 354 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 355 | |
| 356 | _current_process = _MainProcess() |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 357 | _process_counter = itertools.count(1) |
| 358 | _children = set() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 359 | del _MainProcess |
| 360 | |
| 361 | # |
| 362 | # Give names to some return codes |
| 363 | # |
| 364 | |
| 365 | _exitcode_to_name = {} |
| 366 | |
| 367 | for name, signum in list(signal.__dict__.items()): |
| 368 | if name[:3]=='SIG' and '_' not in name: |
| 369 | _exitcode_to_name[-signum] = name |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 370 | |
| 371 | # For debug and leak testing |
| 372 | _dangling = WeakSet() |