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' |
Victor Stinner | 2db6482 | 2017-07-26 02:32:42 +0200 | [diff] [blame] | 151 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 152 | if self._popen is None: |
| 153 | return False |
Victor Stinner | 2db6482 | 2017-07-26 02:32:42 +0200 | [diff] [blame] | 154 | |
| 155 | returncode = self._popen.poll() |
| 156 | if returncode is None: |
| 157 | return True |
| 158 | else: |
| 159 | _children.discard(self) |
| 160 | return False |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 162 | def close(self): |
| 163 | ''' |
| 164 | Close the Process object. |
| 165 | |
| 166 | This method releases resources held by the Process object. It is |
| 167 | an error to call this method if the child process is still running. |
| 168 | ''' |
| 169 | if self._popen is not None: |
| 170 | if self._popen.poll() is None: |
| 171 | raise ValueError("Cannot close a process while it is still running. " |
| 172 | "You should first call join() or terminate().") |
| 173 | self._popen.close() |
| 174 | self._popen = None |
| 175 | del self._sentinel |
| 176 | _children.discard(self) |
| 177 | self._closed = True |
| 178 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 179 | @property |
| 180 | def name(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 181 | return self._name |
| 182 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 183 | @name.setter |
| 184 | def name(self, name): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 185 | assert isinstance(name, str), 'name must be a string' |
| 186 | self._name = name |
| 187 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 188 | @property |
| 189 | def daemon(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 190 | ''' |
| 191 | Return whether process is a daemon |
| 192 | ''' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 193 | return self._config.get('daemon', False) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 194 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 195 | @daemon.setter |
| 196 | def daemon(self, daemonic): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 197 | ''' |
| 198 | Set whether process is a daemon |
| 199 | ''' |
| 200 | assert self._popen is None, 'process has already started' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 201 | self._config['daemon'] = daemonic |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 202 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 203 | @property |
| 204 | def authkey(self): |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 205 | return self._config['authkey'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 206 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 207 | @authkey.setter |
| 208 | def authkey(self, authkey): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 209 | ''' |
| 210 | Set authorization key of process |
| 211 | ''' |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 212 | self._config['authkey'] = AuthenticationString(authkey) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 213 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 214 | @property |
| 215 | def exitcode(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 216 | ''' |
| 217 | Return exit code of process or `None` if it has yet to stop |
| 218 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 219 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 220 | if self._popen is None: |
| 221 | return self._popen |
| 222 | return self._popen.poll() |
| 223 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 224 | @property |
| 225 | def ident(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 226 | ''' |
Florent Xicluna | b519d23 | 2010-03-04 16:10:55 +0000 | [diff] [blame] | 227 | 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] | 228 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 229 | self._check_closed() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 230 | if self is _current_process: |
| 231 | return os.getpid() |
| 232 | else: |
| 233 | return self._popen and self._popen.pid |
| 234 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 235 | pid = ident |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 237 | @property |
| 238 | def sentinel(self): |
| 239 | ''' |
| 240 | Return a file descriptor (Unix) or handle (Windows) suitable for |
| 241 | waiting for process termination. |
| 242 | ''' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 243 | self._check_closed() |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 244 | try: |
| 245 | return self._sentinel |
| 246 | except AttributeError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 247 | raise ValueError("process not started") from None |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 248 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 249 | def __repr__(self): |
| 250 | if self is _current_process: |
| 251 | status = 'started' |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 252 | elif self._closed: |
| 253 | status = 'closed' |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 254 | elif self._parent_pid != os.getpid(): |
| 255 | status = 'unknown' |
| 256 | elif self._popen is None: |
| 257 | status = 'initial' |
| 258 | else: |
| 259 | if self._popen.poll() is not None: |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 260 | status = self.exitcode |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 261 | else: |
| 262 | status = 'started' |
| 263 | |
| 264 | if type(status) is int: |
| 265 | if status == 0: |
| 266 | status = 'stopped' |
| 267 | else: |
| 268 | status = 'stopped[%s]' % _exitcode_to_name.get(status, status) |
| 269 | |
| 270 | return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 271 | status, self.daemon and ' daemon' or '') |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 272 | |
| 273 | ## |
| 274 | |
| 275 | def _bootstrap(self): |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 276 | from . import util, context |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 277 | global _current_process, _process_counter, _children |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 278 | |
| 279 | try: |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 280 | if self._start_method is not None: |
| 281 | context._force_start_method(self._start_method) |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 282 | _process_counter = itertools.count(1) |
| 283 | _children = set() |
Victor Stinner | a6d865c | 2016-03-25 09:29:50 +0100 | [diff] [blame] | 284 | util._close_stdin() |
Victor Stinner | 0f83b15 | 2011-06-17 12:31:49 +0200 | [diff] [blame] | 285 | old_process = _current_process |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 286 | _current_process = self |
Victor Stinner | 0f83b15 | 2011-06-17 12:31:49 +0200 | [diff] [blame] | 287 | try: |
| 288 | util._finalizer_registry.clear() |
| 289 | util._run_after_forkers() |
| 290 | finally: |
| 291 | # delay finalization of the old process object until after |
| 292 | # _run_after_forkers() is executed |
| 293 | del old_process |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 294 | util.info('child process calling self.run()') |
| 295 | try: |
| 296 | self.run() |
| 297 | exitcode = 0 |
| 298 | finally: |
| 299 | util._exit_function() |
| 300 | except SystemExit as e: |
| 301 | if not e.args: |
| 302 | exitcode = 1 |
Richard Oudkerk | 29471de | 2012-06-06 19:04:57 +0100 | [diff] [blame] | 303 | elif isinstance(e.args[0], int): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 304 | exitcode = e.args[0] |
| 305 | else: |
Richard Oudkerk | 29471de | 2012-06-06 19:04:57 +0100 | [diff] [blame] | 306 | sys.stderr.write(str(e.args[0]) + '\n') |
Richard Oudkerk | 8731d7b | 2013-11-17 17:24:11 +0000 | [diff] [blame] | 307 | exitcode = 1 |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 308 | except: |
| 309 | exitcode = 1 |
| 310 | import traceback |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 311 | sys.stderr.write('Process %s:\n' % self.name) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 312 | traceback.print_exc() |
Antoine Pitrou | 84a0fbf | 2012-01-27 10:52:37 +0100 | [diff] [blame] | 313 | finally: |
| 314 | util.info('process exiting with exitcode %d' % exitcode) |
| 315 | sys.stdout.flush() |
| 316 | sys.stderr.flush() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 317 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 318 | return exitcode |
| 319 | |
| 320 | # |
| 321 | # We subclass bytes to avoid accidental transmission of auth keys over network |
| 322 | # |
| 323 | |
| 324 | class AuthenticationString(bytes): |
| 325 | def __reduce__(self): |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 326 | from .context import get_spawning_popen |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 327 | if get_spawning_popen() is None: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 328 | raise TypeError( |
| 329 | 'Pickling an AuthenticationString object is ' |
| 330 | 'disallowed for security reasons' |
| 331 | ) |
| 332 | return AuthenticationString, (bytes(self),) |
| 333 | |
| 334 | # |
| 335 | # Create object representing the main process |
| 336 | # |
| 337 | |
Richard Oudkerk | b1694cf | 2013-10-16 16:41:56 +0100 | [diff] [blame] | 338 | class _MainProcess(BaseProcess): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 339 | |
| 340 | def __init__(self): |
| 341 | self._identity = () |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 342 | self._name = 'MainProcess' |
| 343 | self._parent_pid = None |
| 344 | self._popen = None |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 345 | self._closed = False |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 346 | self._config = {'authkey': AuthenticationString(os.urandom(32)), |
Richard Oudkerk | e943697 | 2013-11-02 17:05:07 +0000 | [diff] [blame] | 347 | 'semprefix': '/mp'} |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 348 | # Note that some versions of FreeBSD only allow named |
Richard Oudkerk | e943697 | 2013-11-02 17:05:07 +0000 | [diff] [blame] | 349 | # semaphores to have names of up to 14 characters. Therefore |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 350 | # we choose a short prefix. |
Richard Oudkerk | e943697 | 2013-11-02 17:05:07 +0000 | [diff] [blame] | 351 | # |
| 352 | # On MacOSX in a sandbox it may be necessary to use a |
| 353 | # different prefix -- see #19478. |
| 354 | # |
| 355 | # Everything in self._config will be inherited by descendant |
| 356 | # processes. |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 357 | |
Antoine Pitrou | 13e96cc | 2017-06-24 19:22:23 +0200 | [diff] [blame] | 358 | def close(self): |
| 359 | pass |
| 360 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 361 | |
| 362 | _current_process = _MainProcess() |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 363 | _process_counter = itertools.count(1) |
| 364 | _children = set() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 365 | del _MainProcess |
| 366 | |
| 367 | # |
| 368 | # Give names to some return codes |
| 369 | # |
| 370 | |
| 371 | _exitcode_to_name = {} |
| 372 | |
| 373 | for name, signum in list(signal.__dict__.items()): |
| 374 | if name[:3]=='SIG' and '_' not in name: |
| 375 | _exitcode_to_name[-signum] = name |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 376 | |
| 377 | # For debug and leak testing |
| 378 | _dangling = WeakSet() |