Back to index

python3.2  3.2.2
Classes | Functions | Variables
multiprocessing.forking Namespace Reference

Classes

class  ForkingPickler
class  _C
class  Popen

Functions

def assert_spawning
def _reduce_method
def _reduce_method_descriptor
def _reduce_partial
def _rebuild_partial
def dump
def set_executable
def duplicate
def is_forking
def freeze_support
def get_command_line
def main
def get_preparation_data
def reduce_connection
def prepare

Variables

list __all__ = ['Popen', 'assert_spawning', 'exit', 'duplicate', 'close', 'ForkingPickler']
 exit = os._exit
 duplicate = os.dup
 close = os.close
int TERMINATE = 0x10000
tuple WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))
tuple WINSERVICE = sys.executable.lower()
tuple _python_exe = os.path.join(sys.exec_prefix, 'python.exe')
list old_main_modules = []

Function Documentation

def multiprocessing.forking._rebuild_partial (   func,
  args,
  keywords 
) [private]

Definition at line 94 of file forking.py.

00094 
00095     def _rebuild_partial(func, args, keywords):
        return partial(func, *args, **keywords)

Here is the call graph for this function:

Definition at line 71 of file forking.py.

00071 
00072 def _reduce_method(m):
00073     if m.__self__ is None:
00074         return getattr, (m.__class__, m.__func__.__name__)
00075     else:
        return getattr, (m.__self__, m.__func__.__name__)

Definition at line 82 of file forking.py.

00082 
00083 def _reduce_method_descriptor(m):
00084     return getattr, (m.__objclass__, m.__name__)
00085 ForkingPickler.register(type(list.append), _reduce_method_descriptor)
00086 ForkingPickler.register(type(int.__add__), _reduce_method_descriptor)
00087 
00088 try:
    from functools import partial

Definition at line 92 of file forking.py.

00092 
00093     def _reduce_partial(p):
        return _rebuild_partial, (p.func, p.args, p.keywords or {})

Definition at line 47 of file forking.py.

00047 
00048 def assert_spawning(self):
00049     if not Popen.thread_is_spawning():
00050         raise RuntimeError(
00051             '%s objects should only be shared between processes'
00052             ' through inheritance' % type(self).__name__
00053             )
00054 
00055 #
00056 # Try making some callable types picklable
00057 #

Here is the caller graph for this function:

def multiprocessing.forking.dump (   obj,
  file,
  protocol = None 
)

Definition at line 189 of file forking.py.

00189 
00190     def dump(obj, file, protocol=None):
00191         ForkingPickler(file, protocol).dump(obj)

def multiprocessing.forking.duplicate (   handle,
  target_process = None,
  inheritable = False 
)

Definition at line 221 of file forking.py.

00221 
00222     def duplicate(handle, target_process=None, inheritable=False):
00223         if target_process is None:
00224             target_process = _subprocess.GetCurrentProcess()
00225         return _subprocess.DuplicateHandle(
00226             _subprocess.GetCurrentProcess(), handle, target_process,
00227             0, inheritable, _subprocess.DUPLICATE_SAME_ACCESS
00228             ).Detach()

Run code for process object if this in not the main process

Definition at line 323 of file forking.py.

00323 
00324     def freeze_support():
00325         '''
00326         Run code for process object if this in not the main process
00327         '''
00328         if is_forking(sys.argv):
00329             main()
00330             sys.exit()
00331 

Here is the call graph for this function:

Returns prefix of command line used for spawning a child process

Definition at line 332 of file forking.py.

00332 
00333     def get_command_line():
00334         '''
00335         Returns prefix of command line used for spawning a child process
00336         '''
00337         if process.current_process()._identity==() and is_forking(sys.argv):
00338             raise RuntimeError('''
00339             Attempt to start a new process before the current process
00340             has finished its bootstrapping phase.
00341 
00342             This probably means that you are on Windows and you have
00343             forgotten to use the proper idiom in the main module:
00344 
00345                 if __name__ == '__main__':
00346                     freeze_support()
00347                     ...
00348 
00349             The "freeze_support()" line can be omitted if the program
00350             is not going to be frozen to produce a Windows executable.''')
00351 
00352         if getattr(sys, 'frozen', False):
00353             return [sys.executable, '--multiprocessing-fork']
00354         else:
00355             prog = 'from multiprocessing.forking import main; main()'
00356             return [_python_exe, '-c', prog, '--multiprocessing-fork']
00357 

Here is the call graph for this function:

Here is the caller graph for this function:

Return info about parent needed by child to unpickle process object

Definition at line 380 of file forking.py.

00380 
00381     def get_preparation_data(name):
00382         '''
00383         Return info about parent needed by child to unpickle process object
00384         '''
00385         from .util import _logger, _log_to_stderr
00386 
00387         d = dict(
00388             name=name,
00389             sys_path=sys.path,
00390             sys_argv=sys.argv,
00391             log_to_stderr=_log_to_stderr,
00392             orig_dir=process.ORIGINAL_DIR,
00393             authkey=process.current_process().authkey,
00394             )
00395 
00396         if _logger is not None:
00397             d['log_level'] = _logger.getEffectiveLevel()
00398 
00399         if not WINEXE and not WINSERVICE:
00400             main_path = getattr(sys.modules['__main__'], '__file__', None)
00401             if not main_path and sys.argv[0] not in ('', '-c'):
00402                 main_path = sys.argv[0]
00403             if main_path is not None:
00404                 if not os.path.isabs(main_path) and \
00405                                           process.ORIGINAL_DIR is not None:
00406                     main_path = os.path.join(process.ORIGINAL_DIR, main_path)
00407                 d['main_path'] = os.path.normpath(main_path)
00408 
00409         return d

Here is the call graph for this function:

Return whether commandline indicates we are forking

Definition at line 312 of file forking.py.

00312 
00313     def is_forking(argv):
00314         '''
00315         Return whether commandline indicates we are forking
00316         '''
00317         if len(argv) >= 2 and argv[1] == '--multiprocessing-fork':
00318             assert len(argv) == 3
00319             return True
00320         else:
00321             return False
00322 

Here is the caller graph for this function:

Run code specifed by data received over pipe

Definition at line 358 of file forking.py.

00358 
00359     def main():
00360         '''
00361         Run code specifed by data received over pipe
00362         '''
00363         assert is_forking(sys.argv)
00364 
00365         handle = int(sys.argv[-1])
00366         fd = msvcrt.open_osfhandle(handle, os.O_RDONLY)
00367         from_parent = os.fdopen(fd, 'rb')
00368 
00369         process.current_process()._inheriting = True
00370         preparation_data = load(from_parent)
00371         prepare(preparation_data)
00372         self = load(from_parent)
00373         process.current_process()._inheriting = False
00374 
00375         from_parent.close()
00376 
00377         exitcode = self._bootstrap()
00378         exit(exitcode)
00379 

Here is the call graph for this function:

Here is the caller graph for this function:

Try to get current process ready to unpickle process object

Definition at line 432 of file forking.py.

00432 
00433 def prepare(data):
00434     '''
00435     Try to get current process ready to unpickle process object
00436     '''
00437     old_main_modules.append(sys.modules['__main__'])
00438 
00439     if 'name' in data:
00440         process.current_process().name = data['name']
00441 
00442     if 'authkey' in data:
00443         process.current_process()._authkey = data['authkey']
00444 
00445     if 'log_to_stderr' in data and data['log_to_stderr']:
00446         util.log_to_stderr()
00447 
00448     if 'log_level' in data:
00449         util.get_logger().setLevel(data['log_level'])
00450 
00451     if 'sys_path' in data:
00452         sys.path = data['sys_path']
00453 
00454     if 'sys_argv' in data:
00455         sys.argv = data['sys_argv']
00456 
00457     if 'dir' in data:
00458         os.chdir(data['dir'])
00459 
00460     if 'orig_dir' in data:
00461         process.ORIGINAL_DIR = data['orig_dir']
00462 
00463     if 'main_path' in data:
00464         # XXX (ncoghlan): The following code makes several bogus
00465         # assumptions regarding the relationship between __file__
00466         # and a module's real name. See PEP 302 and issue #10845
00467         main_path = data['main_path']
00468         main_name = os.path.splitext(os.path.basename(main_path))[0]
00469         if main_name == '__init__':
00470             main_name = os.path.basename(os.path.dirname(main_path))
00471 
00472         if main_name == '__main__':
00473             main_module = sys.modules['__main__']
00474             main_module.__file__ = main_path
00475         elif main_name != 'ipython':
00476             # Main modules not actually called __main__.py may
00477             # contain additional code that should still be executed
00478             import imp
00479 
00480             if main_path is None:
00481                 dirs = None
00482             elif os.path.basename(main_path).startswith('__init__.py'):
00483                 dirs = [os.path.dirname(os.path.dirname(main_path))]
00484             else:
00485                 dirs = [os.path.dirname(main_path)]
00486 
00487             assert main_name not in sys.modules, main_name
00488             file, path_name, etc = imp.find_module(main_name, dirs)
00489             try:
00490                 # We would like to do "imp.load_module('__main__', ...)"
00491                 # here.  However, that would cause 'if __name__ ==
00492                 # "__main__"' clauses to be executed.
00493                 main_module = imp.load_module(
00494                     '__parents_main__', file, path_name, etc
00495                     )
00496             finally:
00497                 if file:
00498                     file.close()
00499 
00500             sys.modules['__main__'] = main_module
00501             main_module.__name__ = '__main__'
00502 
00503             # Try to make the potentially picklable objects in
00504             # sys.modules['__main__'] realize they are in the main
00505             # module -- somewhat ugly.
00506             for obj in list(main_module.__dict__.values()):
00507                 try:
00508                     if obj.__module__ == '__parents_main__':
00509                         obj.__module__ = '__main__'
00510                 except Exception:
00511                     pass

Here is the caller graph for this function:

Definition at line 414 of file forking.py.

00414 
00415     def reduce_connection(conn):
00416         if not Popen.thread_is_spawning():
00417             raise RuntimeError(
00418                 'By default %s objects can only be shared between processes\n'
00419                 'using inheritance' % type(conn).__name__
00420                 )
00421         return type(conn), (Popen.duplicate_for_child(conn.fileno()),
00422                             conn.readable, conn.writable)

Definition at line 213 of file forking.py.

00213 
00214     def set_executable(exe):
00215         global _python_exe
00216         _python_exe = exe


Variable Documentation

Definition at line 41 of file forking.py.

Definition at line 209 of file forking.py.

Definition at line 107 of file forking.py.

Definition at line 106 of file forking.py.

Definition at line 105 of file forking.py.

Definition at line 430 of file forking.py.

Definition at line 196 of file forking.py.

tuple multiprocessing.forking.WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False))

Definition at line 197 of file forking.py.

tuple multiprocessing.forking.WINSERVICE = sys.executable.lower()

Definition at line 198 of file forking.py.