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