2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# Module providing the `Process` class which emulates `threading.Thread`
|
|
|
|
#
|
|
|
|
# multiprocessing/process.py
|
|
|
|
#
|
2010-12-13 21:38:16 -04:00
|
|
|
# Copyright (c) 2006-2008, R Oudkerk
|
2012-04-30 08:13:55 -03:00
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
|
2019-05-20 16:37:05 -03:00
|
|
|
__all__ = ['BaseProcess', 'current_process', 'active_children',
|
|
|
|
'parent_process']
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Imports
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import itertools
|
2017-08-16 15:53:28 -03:00
|
|
|
import threading
|
2011-07-15 17:12:24 -03:00
|
|
|
from _weakrefset import WeakSet
|
2008-06-11 13:44:04 -03: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()
|
2013-08-14 11:35:41 -03:00
|
|
|
return list(_children)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2019-05-20 16:37:05 -03:00
|
|
|
|
|
|
|
def parent_process():
|
|
|
|
'''
|
|
|
|
Return process object representing the parent process
|
|
|
|
'''
|
|
|
|
return _parent_process
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def _cleanup():
|
|
|
|
# check for processes which have finished
|
2013-08-14 11:35:41 -03:00
|
|
|
for p in list(_children):
|
2008-06-11 13:44:04 -03:00
|
|
|
if p._popen.poll() is not None:
|
2013-08-14 11:35:41 -03:00
|
|
|
_children.discard(p)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# The `Process` class
|
|
|
|
#
|
|
|
|
|
2013-10-16 12:41:56 -03:00
|
|
|
class BaseProcess(object):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Process objects represent activity that is run in a separate process
|
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
The class is analogous to `threading.Thread`
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
2013-10-16 12:41:56 -03:00
|
|
|
def _Popen(self):
|
|
|
|
raise NotImplementedError
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2011-02-25 18:07:43 -04:00
|
|
|
def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
|
|
|
|
*, daemon=None):
|
2008-06-11 13:44:04 -03:00
|
|
|
assert group is None, 'group argument must be None for now'
|
2013-08-14 11:35:41 -03:00
|
|
|
count = next(_process_counter)
|
2008-06-11 13:44:04 -03:00
|
|
|
self._identity = _current_process._identity + (count,)
|
2013-08-14 11:35:41 -03:00
|
|
|
self._config = _current_process._config.copy()
|
2008-06-11 13:44:04 -03:00
|
|
|
self._parent_pid = os.getpid()
|
2019-05-20 16:37:05 -03:00
|
|
|
self._parent_name = _current_process.name
|
2008-06-11 13:44:04 -03:00
|
|
|
self._popen = None
|
2017-06-24 14:22:23 -03:00
|
|
|
self._closed = False
|
2008-06-11 13:44:04 -03:00
|
|
|
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)
|
2013-08-14 11:35:41 -03:00
|
|
|
if daemon is not None:
|
|
|
|
self.daemon = daemon
|
2011-07-15 17:12:24 -03:00
|
|
|
_dangling.add(self)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2017-06-24 14:22:23 -03:00
|
|
|
def _check_closed(self):
|
|
|
|
if self._closed:
|
|
|
|
raise ValueError("process object is closed")
|
|
|
|
|
2008-06-11 13:44:04 -03: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
|
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2008-06-11 13:44:04 -03:00
|
|
|
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'
|
2013-08-14 11:35:41 -03:00
|
|
|
assert not _current_process._config.get('daemon'), \
|
2008-06-11 13:44:04 -03:00
|
|
|
'daemonic processes are not allowed to have children'
|
|
|
|
_cleanup()
|
2013-10-16 12:41:56 -03:00
|
|
|
self._popen = self._Popen(self)
|
2011-06-06 14:35:31 -03:00
|
|
|
self._sentinel = self._popen.sentinel
|
2017-06-28 07:29:08 -03:00
|
|
|
# Avoid a refcycle if the target function holds an indirect
|
|
|
|
# reference to the process object (see bpo-30775)
|
|
|
|
del self._target, self._args, self._kwargs
|
2013-08-14 11:35:41 -03:00
|
|
|
_children.add(self)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
'''
|
|
|
|
Terminate process; sends SIGTERM signal or uses TerminateProcess()
|
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2008-06-11 13:44:04 -03:00
|
|
|
self._popen.terminate()
|
|
|
|
|
2017-07-18 12:34:23 -03:00
|
|
|
def kill(self):
|
|
|
|
'''
|
|
|
|
Terminate process; sends SIGKILL signal or uses TerminateProcess()
|
|
|
|
'''
|
|
|
|
self._check_closed()
|
|
|
|
self._popen.kill()
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
def join(self, timeout=None):
|
|
|
|
'''
|
|
|
|
Wait until child process terminates
|
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2008-06-11 13:44:04 -03:00
|
|
|
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:
|
2013-08-14 11:35:41 -03:00
|
|
|
_children.discard(self)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def is_alive(self):
|
|
|
|
'''
|
|
|
|
Return whether process is alive
|
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2008-06-11 13:44:04 -03:00
|
|
|
if self is _current_process:
|
|
|
|
return True
|
|
|
|
assert self._parent_pid == os.getpid(), 'can only test a child process'
|
2017-07-25 21:32:42 -03:00
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
if self._popen is None:
|
|
|
|
return False
|
2017-07-25 21:32:42 -03:00
|
|
|
|
|
|
|
returncode = self._popen.poll()
|
|
|
|
if returncode is None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
_children.discard(self)
|
|
|
|
return False
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2017-06-24 14:22:23 -03:00
|
|
|
def close(self):
|
|
|
|
'''
|
|
|
|
Close the Process object.
|
|
|
|
|
|
|
|
This method releases resources held by the Process object. It is
|
|
|
|
an error to call this method if the child process is still running.
|
|
|
|
'''
|
|
|
|
if self._popen is not None:
|
|
|
|
if self._popen.poll() is None:
|
|
|
|
raise ValueError("Cannot close a process while it is still running. "
|
|
|
|
"You should first call join() or terminate().")
|
|
|
|
self._popen.close()
|
|
|
|
self._popen = None
|
|
|
|
del self._sentinel
|
|
|
|
_children.discard(self)
|
|
|
|
self._closed = True
|
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@property
|
|
|
|
def name(self):
|
2008-06-11 13:44:04 -03:00
|
|
|
return self._name
|
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@name.setter
|
|
|
|
def name(self, name):
|
2008-06-11 13:44:04 -03:00
|
|
|
assert isinstance(name, str), 'name must be a string'
|
|
|
|
self._name = name
|
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@property
|
|
|
|
def daemon(self):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Return whether process is a daemon
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
return self._config.get('daemon', False)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@daemon.setter
|
|
|
|
def daemon(self, daemonic):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Set whether process is a daemon
|
|
|
|
'''
|
|
|
|
assert self._popen is None, 'process has already started'
|
2013-08-14 11:35:41 -03:00
|
|
|
self._config['daemon'] = daemonic
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@property
|
|
|
|
def authkey(self):
|
2013-08-14 11:35:41 -03:00
|
|
|
return self._config['authkey']
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@authkey.setter
|
|
|
|
def authkey(self, authkey):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Set authorization key of process
|
|
|
|
'''
|
2013-08-14 11:35:41 -03:00
|
|
|
self._config['authkey'] = AuthenticationString(authkey)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@property
|
|
|
|
def exitcode(self):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
|
|
|
Return exit code of process or `None` if it has yet to stop
|
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2008-06-11 13:44:04 -03:00
|
|
|
if self._popen is None:
|
|
|
|
return self._popen
|
|
|
|
return self._popen.poll()
|
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
@property
|
|
|
|
def ident(self):
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
2010-03-04 12:10:55 -04:00
|
|
|
Return identifier (PID) of process or `None` if it has yet to start
|
2008-06-11 13:44:04 -03:00
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2008-06-11 13:44:04 -03:00
|
|
|
if self is _current_process:
|
|
|
|
return os.getpid()
|
|
|
|
else:
|
|
|
|
return self._popen and self._popen.pid
|
|
|
|
|
2008-08-19 16:17:39 -03:00
|
|
|
pid = ident
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2011-06-06 14:35:31 -03:00
|
|
|
@property
|
|
|
|
def sentinel(self):
|
|
|
|
'''
|
|
|
|
Return a file descriptor (Unix) or handle (Windows) suitable for
|
|
|
|
waiting for process termination.
|
|
|
|
'''
|
2017-06-24 14:22:23 -03:00
|
|
|
self._check_closed()
|
2011-06-06 14:35:31 -03:00
|
|
|
try:
|
|
|
|
return self._sentinel
|
|
|
|
except AttributeError:
|
2017-04-05 03:37:24 -03:00
|
|
|
raise ValueError("process not started") from None
|
2011-06-06 14:35:31 -03:00
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
def __repr__(self):
|
2018-12-14 07:58:52 -04:00
|
|
|
exitcode = None
|
2008-06-11 13:44:04 -03:00
|
|
|
if self is _current_process:
|
|
|
|
status = 'started'
|
2017-06-24 14:22:23 -03:00
|
|
|
elif self._closed:
|
|
|
|
status = 'closed'
|
2008-06-11 13:44:04 -03:00
|
|
|
elif self._parent_pid != os.getpid():
|
|
|
|
status = 'unknown'
|
|
|
|
elif self._popen is None:
|
|
|
|
status = 'initial'
|
|
|
|
else:
|
2018-12-14 07:58:52 -04:00
|
|
|
exitcode = self._popen.poll()
|
|
|
|
if exitcode is not None:
|
2008-06-11 13:44:04 -03:00
|
|
|
status = 'stopped'
|
|
|
|
else:
|
2018-12-14 07:58:52 -04:00
|
|
|
status = 'started'
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2018-12-14 07:58:52 -04:00
|
|
|
info = [type(self).__name__, 'name=%r' % self._name]
|
|
|
|
if self._popen is not None:
|
|
|
|
info.append('pid=%s' % self._popen.pid)
|
|
|
|
info.append('parent=%s' % self._parent_pid)
|
|
|
|
info.append(status)
|
|
|
|
if exitcode is not None:
|
|
|
|
exitcode = _exitcode_to_name.get(exitcode, exitcode)
|
|
|
|
info.append('exitcode=%s' % exitcode)
|
|
|
|
if self.daemon:
|
|
|
|
info.append('daemon')
|
|
|
|
return '<%s>' % ' '.join(info)
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
##
|
|
|
|
|
2019-05-20 16:37:05 -03:00
|
|
|
def _bootstrap(self, parent_sentinel=None):
|
2013-10-16 12:41:56 -03:00
|
|
|
from . import util, context
|
2019-05-20 16:37:05 -03:00
|
|
|
global _current_process, _parent_process, _process_counter, _children
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
try:
|
2013-10-16 12:41:56 -03:00
|
|
|
if self._start_method is not None:
|
|
|
|
context._force_start_method(self._start_method)
|
2013-08-14 11:35:41 -03:00
|
|
|
_process_counter = itertools.count(1)
|
|
|
|
_children = set()
|
2016-03-25 05:29:50 -03:00
|
|
|
util._close_stdin()
|
2011-06-17 07:31:49 -03:00
|
|
|
old_process = _current_process
|
2008-06-11 13:44:04 -03:00
|
|
|
_current_process = self
|
2019-05-20 16:37:05 -03:00
|
|
|
_parent_process = _ParentProcess(
|
|
|
|
self._parent_name, self._parent_pid, parent_sentinel)
|
2019-11-19 15:50:12 -04:00
|
|
|
if threading._HAVE_THREAD_NATIVE_ID:
|
|
|
|
threading.main_thread()._set_native_id()
|
2011-06-17 07:31:49 -03:00
|
|
|
try:
|
2022-06-09 13:55:12 -03:00
|
|
|
self._after_fork()
|
2011-06-17 07:31:49 -03:00
|
|
|
finally:
|
|
|
|
# delay finalization of the old process object until after
|
|
|
|
# _run_after_forkers() is executed
|
|
|
|
del old_process
|
2008-06-11 13:44:04 -03:00
|
|
|
util.info('child process calling self.run()')
|
|
|
|
try:
|
|
|
|
self.run()
|
|
|
|
exitcode = 0
|
|
|
|
finally:
|
|
|
|
util._exit_function()
|
|
|
|
except SystemExit as e:
|
2020-02-21 05:33:04 -04:00
|
|
|
if e.code is None:
|
|
|
|
exitcode = 0
|
|
|
|
elif isinstance(e.code, int):
|
|
|
|
exitcode = e.code
|
2008-06-11 13:44:04 -03:00
|
|
|
else:
|
2020-02-21 05:33:04 -04:00
|
|
|
sys.stderr.write(str(e.code) + '\n')
|
2013-11-17 13:24:11 -04:00
|
|
|
exitcode = 1
|
2008-06-11 13:44:04 -03:00
|
|
|
except:
|
|
|
|
exitcode = 1
|
|
|
|
import traceback
|
2008-08-19 16:17:39 -03:00
|
|
|
sys.stderr.write('Process %s:\n' % self.name)
|
2008-06-11 13:44:04 -03:00
|
|
|
traceback.print_exc()
|
2012-01-27 05:52:37 -04:00
|
|
|
finally:
|
2017-08-16 15:53:28 -03:00
|
|
|
threading._shutdown()
|
2012-01-27 05:52:37 -04:00
|
|
|
util.info('process exiting with exitcode %d' % exitcode)
|
2018-03-11 15:21:38 -03:00
|
|
|
util._flush_std_streams()
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
return exitcode
|
|
|
|
|
2022-06-09 13:55:12 -03:00
|
|
|
@staticmethod
|
|
|
|
def _after_fork():
|
|
|
|
from . import util
|
|
|
|
util._finalizer_registry.clear()
|
|
|
|
util._run_after_forkers()
|
|
|
|
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# We subclass bytes to avoid accidental transmission of auth keys over network
|
|
|
|
#
|
|
|
|
|
|
|
|
class AuthenticationString(bytes):
|
|
|
|
def __reduce__(self):
|
2013-10-16 12:41:56 -03:00
|
|
|
from .context import get_spawning_popen
|
2013-08-14 11:35:41 -03:00
|
|
|
if get_spawning_popen() is None:
|
2008-06-11 13:44:04 -03:00
|
|
|
raise TypeError(
|
|
|
|
'Pickling an AuthenticationString object is '
|
|
|
|
'disallowed for security reasons'
|
|
|
|
)
|
|
|
|
return AuthenticationString, (bytes(self),)
|
|
|
|
|
2019-05-20 16:37:05 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Create object representing the parent process
|
|
|
|
#
|
|
|
|
|
|
|
|
class _ParentProcess(BaseProcess):
|
|
|
|
|
|
|
|
def __init__(self, name, pid, sentinel):
|
|
|
|
self._identity = ()
|
|
|
|
self._name = name
|
|
|
|
self._pid = pid
|
|
|
|
self._parent_pid = None
|
|
|
|
self._popen = None
|
|
|
|
self._closed = False
|
|
|
|
self._sentinel = sentinel
|
|
|
|
self._config = {}
|
|
|
|
|
|
|
|
def is_alive(self):
|
|
|
|
from multiprocessing.connection import wait
|
|
|
|
return not wait([self._sentinel], timeout=0)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ident(self):
|
|
|
|
return self._pid
|
|
|
|
|
|
|
|
def join(self, timeout=None):
|
|
|
|
'''
|
|
|
|
Wait until parent process terminates
|
|
|
|
'''
|
|
|
|
from multiprocessing.connection import wait
|
|
|
|
wait([self._sentinel], timeout=timeout)
|
|
|
|
|
|
|
|
pid = ident
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#
|
|
|
|
# Create object representing the main process
|
|
|
|
#
|
|
|
|
|
2013-10-16 12:41:56 -03:00
|
|
|
class _MainProcess(BaseProcess):
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._identity = ()
|
|
|
|
self._name = 'MainProcess'
|
|
|
|
self._parent_pid = None
|
|
|
|
self._popen = None
|
2017-06-24 14:22:23 -03:00
|
|
|
self._closed = False
|
2013-08-14 11:35:41 -03:00
|
|
|
self._config = {'authkey': AuthenticationString(os.urandom(32)),
|
2013-11-02 14:05:07 -03:00
|
|
|
'semprefix': '/mp'}
|
2013-08-14 11:35:41 -03:00
|
|
|
# Note that some versions of FreeBSD only allow named
|
2013-11-02 14:05:07 -03:00
|
|
|
# semaphores to have names of up to 14 characters. Therefore
|
2013-08-14 11:35:41 -03:00
|
|
|
# we choose a short prefix.
|
2013-11-02 14:05:07 -03:00
|
|
|
#
|
|
|
|
# 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-08-14 11:35:41 -03:00
|
|
|
|
2017-06-24 14:22:23 -03:00
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2019-05-20 16:37:05 -03:00
|
|
|
_parent_process = None
|
2008-06-11 13:44:04 -03:00
|
|
|
_current_process = _MainProcess()
|
2013-08-14 11:35:41 -03:00
|
|
|
_process_counter = itertools.count(1)
|
|
|
|
_children = set()
|
2008-06-11 13:44:04 -03:00
|
|
|
del _MainProcess
|
|
|
|
|
|
|
|
#
|
|
|
|
# Give names to some return codes
|
|
|
|
#
|
|
|
|
|
|
|
|
_exitcode_to_name = {}
|
|
|
|
|
|
|
|
for name, signum in list(signal.__dict__.items()):
|
|
|
|
if name[:3]=='SIG' and '_' not in name:
|
2018-12-14 07:58:52 -04:00
|
|
|
_exitcode_to_name[-signum] = f'-{name}'
|
2022-02-03 05:20:08 -04:00
|
|
|
del name, signum
|
2011-07-15 17:12:24 -03:00
|
|
|
|
|
|
|
# For debug and leak testing
|
|
|
|
_dangling = WeakSet()
|