2013-10-11 17:28:32 +00:00
|
|
|
#
|
|
|
|
# Module providing the `Process` class which emulates `threading.Thread`
|
|
|
|
#
|
|
|
|
# multiprocessing/process.py
|
|
|
|
#
|
|
|
|
# Copyright (c) 2006-2008, R Oudkerk
|
2014-09-30 16:15:32 +00:00
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
2013-10-11 17:28:32 +00:00
|
|
|
#
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
__all__ = ['BaseProcess', 'current_process', 'active_children']
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Imports
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import itertools
|
2014-09-30 16:15:32 +00:00
|
|
|
from _weakrefset import WeakSet
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
try:
|
|
|
|
ORIGINAL_DIR = os.path.abspath(os.getcwd())
|
|
|
|
except OSError:
|
|
|
|
ORIGINAL_DIR = None
|
|
|
|
|
|
|
|
#
|
|
|
|
# Public functions
|
|
|
|
#
|
|
|
|
|
|
|
|
def current_process():
|
|
|
|
'''
|
|
|
|
Return process object representing the current process
|
|
|
|
'''
|
|
|
|
return _current_process
|
|
|
|
|
|
|
|
def active_children():
|
|
|
|
'''
|
|
|
|
Return list of process objects corresponding to live child processes
|
|
|
|
'''
|
|
|
|
_cleanup()
|
2014-09-30 16:15:32 +00:00
|
|
|
return list(_children)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def _cleanup():
|
|
|
|
# check for processes which have finished
|
2014-09-30 16:15:32 +00:00
|
|
|
for p in list(_children):
|
2013-10-11 17:28:32 +00:00
|
|
|
if p._popen.poll() is not None:
|
2014-09-30 16:15:32 +00:00
|
|
|
_children.discard(p)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# The `Process` class
|
|
|
|
#
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
class BaseProcess(object):
|
2013-10-11 17:28:32 +00:00
|
|
|
'''
|
|
|
|
Process objects represent activity that is run in a separate process
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
The class is analogous to `threading.Thread`
|
2013-10-11 17:28:32 +00:00
|
|
|
'''
|
2014-09-30 16:15:32 +00:00
|
|
|
def _Popen(self):
|
|
|
|
raise NotImplementedError
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
|
|
|
|
*, daemon=None):
|
2013-10-11 17:28:32 +00:00
|
|
|
assert group is None, 'group argument must be None for now'
|
2014-09-30 16:15:32 +00:00
|
|
|
count = next(_process_counter)
|
2013-10-11 17:28:32 +00:00
|
|
|
self._identity = _current_process._identity + (count,)
|
2014-09-30 16:15:32 +00:00
|
|
|
self._config = _current_process._config.copy()
|
2013-10-11 17:28:32 +00:00
|
|
|
self._parent_pid = os.getpid()
|
|
|
|
self._popen = None
|
|
|
|
self._target = target
|
|
|
|
self._args = tuple(args)
|
|
|
|
self._kwargs = dict(kwargs)
|
|
|
|
self._name = name or type(self).__name__ + '-' + \
|
|
|
|
':'.join(str(i) for i in self._identity)
|
2014-09-30 16:15:32 +00:00
|
|
|
if daemon is not None:
|
|
|
|
self.daemon = daemon
|
|
|
|
_dangling.add(self)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
'''
|
|
|
|
Method to be run in sub-process; can be overridden in sub-class
|
|
|
|
'''
|
|
|
|
if self._target:
|
|
|
|
self._target(*self._args, **self._kwargs)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
'''
|
|
|
|
Start child process
|
|
|
|
'''
|
|
|
|
assert self._popen is None, 'cannot start a process twice'
|
|
|
|
assert self._parent_pid == os.getpid(), \
|
|
|
|
'can only start a process object created by current process'
|
2014-09-30 16:15:32 +00:00
|
|
|
assert not _current_process._config.get('daemon'), \
|
2013-10-11 17:28:32 +00:00
|
|
|
'daemonic processes are not allowed to have children'
|
|
|
|
_cleanup()
|
2014-09-30 16:15:32 +00:00
|
|
|
self._popen = self._Popen(self)
|
|
|
|
self._sentinel = self._popen.sentinel
|
|
|
|
_children.add(self)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
'''
|
|
|
|
Terminate process; sends SIGTERM signal or uses TerminateProcess()
|
|
|
|
'''
|
|
|
|
self._popen.terminate()
|
|
|
|
|
|
|
|
def join(self, timeout=None):
|
|
|
|
'''
|
|
|
|
Wait until child process terminates
|
|
|
|
'''
|
|
|
|
assert self._parent_pid == os.getpid(), 'can only join a child process'
|
|
|
|
assert self._popen is not None, 'can only join a started process'
|
|
|
|
res = self._popen.wait(timeout)
|
|
|
|
if res is not None:
|
2014-09-30 16:15:32 +00:00
|
|
|
_children.discard(self)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def is_alive(self):
|
|
|
|
'''
|
|
|
|
Return whether process is alive
|
|
|
|
'''
|
|
|
|
if self is _current_process:
|
|
|
|
return True
|
|
|
|
assert self._parent_pid == os.getpid(), 'can only test a child process'
|
|
|
|
if self._popen is None:
|
|
|
|
return False
|
|
|
|
self._popen.poll()
|
|
|
|
return self._popen.returncode is None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, name):
|
2014-09-30 16:15:32 +00:00
|
|
|
assert isinstance(name, str), 'name must be a string'
|
2013-10-11 17:28:32 +00:00
|
|
|
self._name = name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def daemon(self):
|
|
|
|
'''
|
|
|
|
Return whether process is a daemon
|
|
|
|
'''
|
2014-09-30 16:15:32 +00:00
|
|
|
return self._config.get('daemon', False)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
@daemon.setter
|
|
|
|
def daemon(self, daemonic):
|
|
|
|
'''
|
|
|
|
Set whether process is a daemon
|
|
|
|
'''
|
|
|
|
assert self._popen is None, 'process has already started'
|
2014-09-30 16:15:32 +00:00
|
|
|
self._config['daemon'] = daemonic
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def authkey(self):
|
2014-09-30 16:15:32 +00:00
|
|
|
return self._config['authkey']
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
@authkey.setter
|
|
|
|
def authkey(self, authkey):
|
|
|
|
'''
|
|
|
|
Set authorization key of process
|
|
|
|
'''
|
2014-09-30 16:15:32 +00:00
|
|
|
self._config['authkey'] = AuthenticationString(authkey)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def exitcode(self):
|
|
|
|
'''
|
|
|
|
Return exit code of process or `None` if it has yet to stop
|
|
|
|
'''
|
|
|
|
if self._popen is None:
|
|
|
|
return self._popen
|
|
|
|
return self._popen.poll()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ident(self):
|
|
|
|
'''
|
|
|
|
Return identifier (PID) of process or `None` if it has yet to start
|
|
|
|
'''
|
|
|
|
if self is _current_process:
|
|
|
|
return os.getpid()
|
|
|
|
else:
|
|
|
|
return self._popen and self._popen.pid
|
|
|
|
|
|
|
|
pid = ident
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
@property
|
|
|
|
def sentinel(self):
|
|
|
|
'''
|
|
|
|
Return a file descriptor (Unix) or handle (Windows) suitable for
|
|
|
|
waiting for process termination.
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
return self._sentinel
|
|
|
|
except AttributeError:
|
|
|
|
raise ValueError("process not started")
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
def __repr__(self):
|
|
|
|
if self is _current_process:
|
|
|
|
status = 'started'
|
|
|
|
elif self._parent_pid != os.getpid():
|
|
|
|
status = 'unknown'
|
|
|
|
elif self._popen is None:
|
|
|
|
status = 'initial'
|
|
|
|
else:
|
|
|
|
if self._popen.poll() is not None:
|
|
|
|
status = self.exitcode
|
|
|
|
else:
|
|
|
|
status = 'started'
|
|
|
|
|
|
|
|
if type(status) is int:
|
|
|
|
if status == 0:
|
|
|
|
status = 'stopped'
|
|
|
|
else:
|
|
|
|
status = 'stopped[%s]' % _exitcode_to_name.get(status, status)
|
|
|
|
|
|
|
|
return '<%s(%s, %s%s)>' % (type(self).__name__, self._name,
|
2014-09-30 16:15:32 +00:00
|
|
|
status, self.daemon and ' daemon' or '')
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
|
|
|
|
def _bootstrap(self):
|
2014-09-30 16:15:32 +00:00
|
|
|
from . import util, context
|
|
|
|
global _current_process, _process_counter, _children
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
try:
|
2014-09-30 16:15:32 +00:00
|
|
|
if self._start_method is not None:
|
|
|
|
context._force_start_method(self._start_method)
|
|
|
|
_process_counter = itertools.count(1)
|
|
|
|
_children = set()
|
|
|
|
if sys.stdin is not None:
|
|
|
|
try:
|
|
|
|
sys.stdin.close()
|
|
|
|
sys.stdin = open(os.devnull)
|
|
|
|
except (OSError, ValueError):
|
|
|
|
pass
|
|
|
|
old_process = _current_process
|
2013-10-11 17:28:32 +00:00
|
|
|
_current_process = self
|
2014-09-30 16:15:32 +00:00
|
|
|
try:
|
|
|
|
util._finalizer_registry.clear()
|
|
|
|
util._run_after_forkers()
|
|
|
|
finally:
|
|
|
|
# delay finalization of the old process object until after
|
|
|
|
# _run_after_forkers() is executed
|
|
|
|
del old_process
|
2013-10-11 17:28:32 +00:00
|
|
|
util.info('child process calling self.run()')
|
|
|
|
try:
|
|
|
|
self.run()
|
|
|
|
exitcode = 0
|
|
|
|
finally:
|
|
|
|
util._exit_function()
|
2014-09-30 16:15:32 +00:00
|
|
|
except SystemExit as e:
|
2013-10-11 17:28:32 +00:00
|
|
|
if not e.args:
|
|
|
|
exitcode = 1
|
|
|
|
elif isinstance(e.args[0], int):
|
|
|
|
exitcode = e.args[0]
|
|
|
|
else:
|
|
|
|
sys.stderr.write(str(e.args[0]) + '\n')
|
2014-09-30 16:15:32 +00:00
|
|
|
exitcode = 1
|
2013-10-11 17:28:32 +00:00
|
|
|
except:
|
|
|
|
exitcode = 1
|
|
|
|
import traceback
|
|
|
|
sys.stderr.write('Process %s:\n' % self.name)
|
|
|
|
traceback.print_exc()
|
2014-09-30 16:15:32 +00:00
|
|
|
finally:
|
|
|
|
util.info('process exiting with exitcode %d' % exitcode)
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
return exitcode
|
|
|
|
|
|
|
|
#
|
|
|
|
# We subclass bytes to avoid accidental transmission of auth keys over network
|
|
|
|
#
|
|
|
|
|
|
|
|
class AuthenticationString(bytes):
|
|
|
|
def __reduce__(self):
|
2014-09-30 16:15:32 +00:00
|
|
|
from .context import get_spawning_popen
|
|
|
|
if get_spawning_popen() is None:
|
2013-10-11 17:28:32 +00:00
|
|
|
raise TypeError(
|
|
|
|
'Pickling an AuthenticationString object is '
|
|
|
|
'disallowed for security reasons'
|
|
|
|
)
|
|
|
|
return AuthenticationString, (bytes(self),)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create object representing the main process
|
|
|
|
#
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
class _MainProcess(BaseProcess):
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._identity = ()
|
|
|
|
self._name = 'MainProcess'
|
|
|
|
self._parent_pid = None
|
|
|
|
self._popen = None
|
2014-09-30 16:15:32 +00:00
|
|
|
self._config = {'authkey': AuthenticationString(os.urandom(32)),
|
|
|
|
'semprefix': '/mp'}
|
|
|
|
# Note that some versions of FreeBSD only allow named
|
|
|
|
# semaphores to have names of up to 14 characters. Therefore
|
|
|
|
# we choose a short prefix.
|
|
|
|
#
|
|
|
|
# On MacOSX in a sandbox it may be necessary to use a
|
|
|
|
# different prefix -- see #19478.
|
|
|
|
#
|
|
|
|
# Everything in self._config will be inherited by descendant
|
|
|
|
# processes.
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
_current_process = _MainProcess()
|
2014-09-30 16:15:32 +00:00
|
|
|
_process_counter = itertools.count(1)
|
|
|
|
_children = set()
|
2013-10-11 17:28:32 +00:00
|
|
|
del _MainProcess
|
|
|
|
|
|
|
|
#
|
|
|
|
# Give names to some return codes
|
|
|
|
#
|
|
|
|
|
|
|
|
_exitcode_to_name = {}
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
for name, signum in list(signal.__dict__.items()):
|
2013-10-11 17:28:32 +00:00
|
|
|
if name[:3]=='SIG' and '_' not in name:
|
|
|
|
_exitcode_to_name[-signum] = name
|
2014-09-30 16:15:32 +00:00
|
|
|
|
|
|
|
# For debug and leak testing
|
|
|
|
_dangling = WeakSet()
|