What does the term bootstrap mean in Python Multiprocessing.Process Module?
Playback with process and pool modules in the Multiprocessing package and continue method references _bootstrap
.
From what I can see, the method imports the module multiprocessing
util.py
and uses its methods (in combination with several others) in the child processes trace
, log
and manage
.
def _bootstrap(self):
from . import util
global _current_process
try:
self._children = set()
self._counter = itertools.count(1)
try:
sys.stdin.close()
sys.stdin = open(os.devnull)
except (OSError, ValueError):
pass
_current_process = self
util._finalizer_registry.clear()
util._run_after_forkers()
util.info('child process calling self.run()')
try:
self.run()
exitcode = 0
finally:
util._exit_function()
except SystemExit, e:
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')
sys.stderr.flush()
exitcode = 1
except:
exitcode = 1
import traceback
sys.stderr.write('Process %s:\n' % self.name)
sys.stderr.flush()
traceback.print_exc()
util.info('process exiting with exitcode %d' % exitcode)
return exitcode
Did a little research on the term "bootstrap" and saw that it is used in various contexts in Python modules and most prominently (other than perhaps a beta break) in relation to the process of measuring statistics accuracy.
But in this case, it seems like the function name can refer to a method that binds the various elements together? Is this the case?
source to share
A function _bootstrap
is what is executed internally multiprocessing.Process
immediately after its creation. Or after forking :
def _launch(self, process_obj):
code = 1
parent_r, child_w = os.pipe()
self.pid = os.fork()
if self.pid == 0:
try:
os.close(parent_r)
if 'random' in sys.modules:
import random
random.seed()
code = process_obj._bootstrap()
finally:
os._exit(code)
else:
os.close(child_w)
util.Finalize(self, os.close, (parent_r,))
self.sentinel = parent_r
Or after it appears :
def _main(fd):
with os.fdopen(fd, 'rb', closefd=True) as from_parent:
process.current_process()._inheriting = True
try:
preparation_data = pickle.load(from_parent)
prepare(preparation_data)
self = pickle.load(from_parent)
finally:
del process.current_process()._inheriting
return self._bootstrap()
target
passed to Process
is made from _bootstrap
:
try:
self.run() # This runs target.
exitcode = 0
finally:
util._exit_function()
So "bootstrap" in this context refers to booting (or, more often, these days, booting) a computer . As is the case, the first thing done at startup is which is responsible for launching the software you are really interested in. For multiprocessing
_bootstrap
is responsible for doing the configuration required to run your function target
and then cleaning up after that.
source to share