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 |
| 7 | # All rights reserved. |
| 8 | # |
| 9 | # Redistribution and use in source and binary forms, with or without |
| 10 | # modification, are permitted provided that the following conditions |
| 11 | # are met: |
| 12 | # |
| 13 | # 1. Redistributions of source code must retain the above copyright |
| 14 | # notice, this list of conditions and the following disclaimer. |
| 15 | # 2. Redistributions in binary form must reproduce the above copyright |
| 16 | # notice, this list of conditions and the following disclaimer in the |
| 17 | # documentation and/or other materials provided with the distribution. |
| 18 | # 3. Neither the name of author nor the names of any contributors may be |
| 19 | # used to endorse or promote products derived from this software |
| 20 | # without specific prior written permission. |
| 21 | # |
| 22 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
| 23 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 26 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | # SUCH DAMAGE. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 33 | # |
| 34 | |
| 35 | __all__ = ['Process', 'current_process', 'active_children'] |
| 36 | |
| 37 | # |
| 38 | # Imports |
| 39 | # |
| 40 | |
| 41 | import os |
| 42 | import sys |
| 43 | import signal |
| 44 | import itertools |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 45 | from _weakrefset import WeakSet |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 46 | |
| 47 | # |
| 48 | # |
| 49 | # |
| 50 | |
| 51 | try: |
| 52 | ORIGINAL_DIR = os.path.abspath(os.getcwd()) |
| 53 | except OSError: |
| 54 | ORIGINAL_DIR = None |
| 55 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 56 | # |
| 57 | # Public functions |
| 58 | # |
| 59 | |
| 60 | def current_process(): |
| 61 | ''' |
| 62 | Return process object representing the current process |
| 63 | ''' |
| 64 | return _current_process |
| 65 | |
| 66 | def active_children(): |
| 67 | ''' |
| 68 | Return list of process objects corresponding to live child processes |
| 69 | ''' |
| 70 | _cleanup() |
| 71 | return list(_current_process._children) |
| 72 | |
| 73 | # |
| 74 | # |
| 75 | # |
| 76 | |
| 77 | def _cleanup(): |
| 78 | # check for processes which have finished |
| 79 | for p in list(_current_process._children): |
| 80 | if p._popen.poll() is not None: |
| 81 | _current_process._children.discard(p) |
| 82 | |
| 83 | # |
| 84 | # The `Process` class |
| 85 | # |
| 86 | |
| 87 | class Process(object): |
| 88 | ''' |
| 89 | Process objects represent activity that is run in a separate process |
| 90 | |
| 91 | The class is analagous to `threading.Thread` |
| 92 | ''' |
| 93 | _Popen = None |
| 94 | |
| 95 | def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): |
| 96 | assert group is None, 'group argument must be None for now' |
| 97 | count = next(_current_process._counter) |
| 98 | self._identity = _current_process._identity + (count,) |
| 99 | self._authkey = _current_process._authkey |
| 100 | self._daemonic = _current_process._daemonic |
| 101 | self._tempdir = _current_process._tempdir |
| 102 | self._parent_pid = os.getpid() |
| 103 | self._popen = None |
| 104 | self._target = target |
| 105 | self._args = tuple(args) |
| 106 | self._kwargs = dict(kwargs) |
| 107 | self._name = name or type(self).__name__ + '-' + \ |
| 108 | ':'.join(str(i) for i in self._identity) |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 109 | _dangling.add(self) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 110 | |
| 111 | def run(self): |
| 112 | ''' |
| 113 | Method to be run in sub-process; can be overridden in sub-class |
| 114 | ''' |
| 115 | if self._target: |
| 116 | self._target(*self._args, **self._kwargs) |
| 117 | |
| 118 | def start(self): |
| 119 | ''' |
| 120 | Start child process |
| 121 | ''' |
| 122 | assert self._popen is None, 'cannot start a process twice' |
| 123 | assert self._parent_pid == os.getpid(), \ |
| 124 | 'can only start a process object created by current process' |
| 125 | assert not _current_process._daemonic, \ |
| 126 | 'daemonic processes are not allowed to have children' |
| 127 | _cleanup() |
| 128 | if self._Popen is not None: |
| 129 | Popen = self._Popen |
| 130 | else: |
| 131 | from .forking import Popen |
| 132 | self._popen = Popen(self) |
| 133 | _current_process._children.add(self) |
| 134 | |
| 135 | def terminate(self): |
| 136 | ''' |
| 137 | Terminate process; sends SIGTERM signal or uses TerminateProcess() |
| 138 | ''' |
| 139 | self._popen.terminate() |
| 140 | |
| 141 | def join(self, timeout=None): |
| 142 | ''' |
| 143 | Wait until child process terminates |
| 144 | ''' |
| 145 | assert self._parent_pid == os.getpid(), 'can only join a child process' |
| 146 | assert self._popen is not None, 'can only join a started process' |
| 147 | res = self._popen.wait(timeout) |
| 148 | if res is not None: |
| 149 | _current_process._children.discard(self) |
| 150 | |
| 151 | def is_alive(self): |
| 152 | ''' |
| 153 | Return whether process is alive |
| 154 | ''' |
| 155 | if self is _current_process: |
| 156 | return True |
| 157 | assert self._parent_pid == os.getpid(), 'can only test a child process' |
| 158 | if self._popen is None: |
| 159 | return False |
| 160 | self._popen.poll() |
| 161 | return self._popen.returncode is None |
| 162 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 163 | @property |
| 164 | def name(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 165 | return self._name |
| 166 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 167 | @name.setter |
| 168 | def name(self, name): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 169 | assert isinstance(name, str), 'name must be a string' |
| 170 | self._name = name |
| 171 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 172 | @property |
| 173 | def daemon(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 174 | ''' |
| 175 | Return whether process is a daemon |
| 176 | ''' |
| 177 | return self._daemonic |
| 178 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 179 | @daemon.setter |
| 180 | def daemon(self, daemonic): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 181 | ''' |
| 182 | Set whether process is a daemon |
| 183 | ''' |
| 184 | assert self._popen is None, 'process has already started' |
| 185 | self._daemonic = daemonic |
| 186 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 187 | @property |
| 188 | def authkey(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 189 | return self._authkey |
| 190 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 191 | @authkey.setter |
| 192 | def authkey(self, authkey): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 193 | ''' |
| 194 | Set authorization key of process |
| 195 | ''' |
| 196 | self._authkey = AuthenticationString(authkey) |
| 197 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 198 | @property |
| 199 | def exitcode(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 200 | ''' |
| 201 | Return exit code of process or `None` if it has yet to stop |
| 202 | ''' |
| 203 | if self._popen is None: |
| 204 | return self._popen |
| 205 | return self._popen.poll() |
| 206 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 207 | @property |
| 208 | def ident(self): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 209 | ''' |
Florent Xicluna | b519d23 | 2010-03-04 16:10:55 +0000 | [diff] [blame] | 210 | 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] | 211 | ''' |
| 212 | if self is _current_process: |
| 213 | return os.getpid() |
| 214 | else: |
| 215 | return self._popen and self._popen.pid |
| 216 | |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 217 | pid = ident |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 218 | |
| 219 | def __repr__(self): |
| 220 | if self is _current_process: |
| 221 | status = 'started' |
| 222 | elif self._parent_pid != os.getpid(): |
| 223 | status = 'unknown' |
| 224 | elif self._popen is None: |
| 225 | status = 'initial' |
| 226 | else: |
| 227 | if self._popen.poll() is not None: |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 228 | status = self.exitcode |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 229 | else: |
| 230 | status = 'started' |
| 231 | |
| 232 | if type(status) is int: |
| 233 | if status == 0: |
| 234 | status = 'stopped' |
| 235 | else: |
| 236 | status = 'stopped[%s]' % _exitcode_to_name.get(status, status) |
| 237 | |
| 238 | return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, |
| 239 | status, self._daemonic and ' daemon' or '') |
| 240 | |
| 241 | ## |
| 242 | |
| 243 | def _bootstrap(self): |
| 244 | from . import util |
| 245 | global _current_process |
| 246 | |
| 247 | try: |
| 248 | self._children = set() |
| 249 | self._counter = itertools.count(1) |
Amaury Forgeot d'Arc | 768008c | 2008-08-20 09:04:46 +0000 | [diff] [blame] | 250 | if sys.stdin is not None: |
| 251 | try: |
Alexandre Vassalotti | c57a84f | 2009-07-17 12:07:01 +0000 | [diff] [blame] | 252 | sys.stdin.close() |
| 253 | sys.stdin = open(os.devnull) |
Amaury Forgeot d'Arc | 768008c | 2008-08-20 09:04:46 +0000 | [diff] [blame] | 254 | except (OSError, ValueError): |
| 255 | pass |
Victor Stinner | 0f83b15 | 2011-06-17 12:31:49 +0200 | [diff] [blame] | 256 | old_process = _current_process |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 257 | _current_process = self |
Victor Stinner | 0f83b15 | 2011-06-17 12:31:49 +0200 | [diff] [blame] | 258 | try: |
| 259 | util._finalizer_registry.clear() |
| 260 | util._run_after_forkers() |
| 261 | finally: |
| 262 | # delay finalization of the old process object until after |
| 263 | # _run_after_forkers() is executed |
| 264 | del old_process |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 265 | util.info('child process calling self.run()') |
| 266 | try: |
| 267 | self.run() |
| 268 | exitcode = 0 |
| 269 | finally: |
| 270 | util._exit_function() |
| 271 | except SystemExit as e: |
| 272 | if not e.args: |
| 273 | exitcode = 1 |
| 274 | elif type(e.args[0]) is int: |
| 275 | exitcode = e.args[0] |
| 276 | else: |
| 277 | sys.stderr.write(e.args[0] + '\n') |
| 278 | sys.stderr.flush() |
| 279 | exitcode = 1 |
| 280 | except: |
| 281 | exitcode = 1 |
| 282 | import traceback |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 283 | sys.stderr.write('Process %s:\n' % self.name) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 284 | sys.stderr.flush() |
| 285 | traceback.print_exc() |
| 286 | |
| 287 | util.info('process exiting with exitcode %d' % exitcode) |
| 288 | return exitcode |
| 289 | |
| 290 | # |
| 291 | # We subclass bytes to avoid accidental transmission of auth keys over network |
| 292 | # |
| 293 | |
| 294 | class AuthenticationString(bytes): |
| 295 | def __reduce__(self): |
| 296 | from .forking import Popen |
| 297 | if not Popen.thread_is_spawning(): |
| 298 | raise TypeError( |
| 299 | 'Pickling an AuthenticationString object is ' |
| 300 | 'disallowed for security reasons' |
| 301 | ) |
| 302 | return AuthenticationString, (bytes(self),) |
| 303 | |
| 304 | # |
| 305 | # Create object representing the main process |
| 306 | # |
| 307 | |
| 308 | class _MainProcess(Process): |
| 309 | |
| 310 | def __init__(self): |
| 311 | self._identity = () |
| 312 | self._daemonic = False |
| 313 | self._name = 'MainProcess' |
| 314 | self._parent_pid = None |
| 315 | self._popen = None |
| 316 | self._counter = itertools.count(1) |
| 317 | self._children = set() |
| 318 | self._authkey = AuthenticationString(os.urandom(32)) |
| 319 | self._tempdir = None |
| 320 | |
| 321 | _current_process = _MainProcess() |
| 322 | del _MainProcess |
| 323 | |
| 324 | # |
| 325 | # Give names to some return codes |
| 326 | # |
| 327 | |
| 328 | _exitcode_to_name = {} |
| 329 | |
| 330 | for name, signum in list(signal.__dict__.items()): |
| 331 | if name[:3]=='SIG' and '_' not in name: |
| 332 | _exitcode_to_name[-signum] = name |
Antoine Pitrou | c081c0c | 2011-07-15 22:12:24 +0200 | [diff] [blame] | 333 | |
| 334 | # For debug and leak testing |
| 335 | _dangling = WeakSet() |