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