| 1 | n/a | # |
|---|
| 2 | n/a | # Code used to start processes when using the spawn or forkserver |
|---|
| 3 | n/a | # start methods. |
|---|
| 4 | n/a | # |
|---|
| 5 | n/a | # multiprocessing/spawn.py |
|---|
| 6 | n/a | # |
|---|
| 7 | n/a | # Copyright (c) 2006-2008, R Oudkerk |
|---|
| 8 | n/a | # Licensed to PSF under a Contributor Agreement. |
|---|
| 9 | n/a | # |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | import os |
|---|
| 12 | n/a | import sys |
|---|
| 13 | n/a | import runpy |
|---|
| 14 | n/a | import types |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | from . import get_start_method, set_start_method |
|---|
| 17 | n/a | from . import process |
|---|
| 18 | n/a | from .context import reduction |
|---|
| 19 | n/a | from . import util |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | __all__ = ['_main', 'freeze_support', 'set_executable', 'get_executable', |
|---|
| 22 | n/a | 'get_preparation_data', 'get_command_line', 'import_main_path'] |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | # |
|---|
| 25 | n/a | # _python_exe is the assumed path to the python executable. |
|---|
| 26 | n/a | # People embedding Python want to modify it. |
|---|
| 27 | n/a | # |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | if sys.platform != 'win32': |
|---|
| 30 | n/a | WINEXE = False |
|---|
| 31 | n/a | WINSERVICE = False |
|---|
| 32 | n/a | else: |
|---|
| 33 | n/a | WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False)) |
|---|
| 34 | n/a | WINSERVICE = sys.executable.lower().endswith("pythonservice.exe") |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | if WINSERVICE: |
|---|
| 37 | n/a | _python_exe = os.path.join(sys.exec_prefix, 'python.exe') |
|---|
| 38 | n/a | else: |
|---|
| 39 | n/a | _python_exe = sys.executable |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def set_executable(exe): |
|---|
| 42 | n/a | global _python_exe |
|---|
| 43 | n/a | _python_exe = exe |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | def get_executable(): |
|---|
| 46 | n/a | return _python_exe |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | # |
|---|
| 49 | n/a | # |
|---|
| 50 | n/a | # |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def is_forking(argv): |
|---|
| 53 | n/a | ''' |
|---|
| 54 | n/a | Return whether commandline indicates we are forking |
|---|
| 55 | n/a | ''' |
|---|
| 56 | n/a | if len(argv) >= 2 and argv[1] == '--multiprocessing-fork': |
|---|
| 57 | n/a | return True |
|---|
| 58 | n/a | else: |
|---|
| 59 | n/a | return False |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def freeze_support(): |
|---|
| 63 | n/a | ''' |
|---|
| 64 | n/a | Run code for process object if this in not the main process |
|---|
| 65 | n/a | ''' |
|---|
| 66 | n/a | if is_forking(sys.argv): |
|---|
| 67 | n/a | kwds = {} |
|---|
| 68 | n/a | for arg in sys.argv[2:]: |
|---|
| 69 | n/a | name, value = arg.split('=') |
|---|
| 70 | n/a | if value == 'None': |
|---|
| 71 | n/a | kwds[name] = None |
|---|
| 72 | n/a | else: |
|---|
| 73 | n/a | kwds[name] = int(value) |
|---|
| 74 | n/a | spawn_main(**kwds) |
|---|
| 75 | n/a | sys.exit() |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def get_command_line(**kwds): |
|---|
| 79 | n/a | ''' |
|---|
| 80 | n/a | Returns prefix of command line used for spawning a child process |
|---|
| 81 | n/a | ''' |
|---|
| 82 | n/a | if getattr(sys, 'frozen', False): |
|---|
| 83 | n/a | return ([sys.executable, '--multiprocessing-fork'] + |
|---|
| 84 | n/a | ['%s=%r' % item for item in kwds.items()]) |
|---|
| 85 | n/a | else: |
|---|
| 86 | n/a | prog = 'from multiprocessing.spawn import spawn_main; spawn_main(%s)' |
|---|
| 87 | n/a | prog %= ', '.join('%s=%r' % item for item in kwds.items()) |
|---|
| 88 | n/a | opts = util._args_from_interpreter_flags() |
|---|
| 89 | n/a | return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork'] |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def spawn_main(pipe_handle, parent_pid=None, tracker_fd=None): |
|---|
| 93 | n/a | ''' |
|---|
| 94 | n/a | Run code specified by data received over pipe |
|---|
| 95 | n/a | ''' |
|---|
| 96 | n/a | assert is_forking(sys.argv) |
|---|
| 97 | n/a | if sys.platform == 'win32': |
|---|
| 98 | n/a | import msvcrt |
|---|
| 99 | n/a | new_handle = reduction.steal_handle(parent_pid, pipe_handle) |
|---|
| 100 | n/a | fd = msvcrt.open_osfhandle(new_handle, os.O_RDONLY) |
|---|
| 101 | n/a | else: |
|---|
| 102 | n/a | from . import semaphore_tracker |
|---|
| 103 | n/a | semaphore_tracker._semaphore_tracker._fd = tracker_fd |
|---|
| 104 | n/a | fd = pipe_handle |
|---|
| 105 | n/a | exitcode = _main(fd) |
|---|
| 106 | n/a | sys.exit(exitcode) |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | def _main(fd): |
|---|
| 110 | n/a | with os.fdopen(fd, 'rb', closefd=True) as from_parent: |
|---|
| 111 | n/a | process.current_process()._inheriting = True |
|---|
| 112 | n/a | try: |
|---|
| 113 | n/a | preparation_data = reduction.pickle.load(from_parent) |
|---|
| 114 | n/a | prepare(preparation_data) |
|---|
| 115 | n/a | self = reduction.pickle.load(from_parent) |
|---|
| 116 | n/a | finally: |
|---|
| 117 | n/a | del process.current_process()._inheriting |
|---|
| 118 | n/a | return self._bootstrap() |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def _check_not_importing_main(): |
|---|
| 122 | n/a | if getattr(process.current_process(), '_inheriting', False): |
|---|
| 123 | n/a | raise RuntimeError(''' |
|---|
| 124 | n/a | An attempt has been made to start a new process before the |
|---|
| 125 | n/a | current process has finished its bootstrapping phase. |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | This probably means that you are not using fork to start your |
|---|
| 128 | n/a | child processes and you have forgotten to use the proper idiom |
|---|
| 129 | n/a | in the main module: |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | if __name__ == '__main__': |
|---|
| 132 | n/a | freeze_support() |
|---|
| 133 | n/a | ... |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | The "freeze_support()" line can be omitted if the program |
|---|
| 136 | n/a | is not going to be frozen to produce an executable.''') |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def get_preparation_data(name): |
|---|
| 140 | n/a | ''' |
|---|
| 141 | n/a | Return info about parent needed by child to unpickle process object |
|---|
| 142 | n/a | ''' |
|---|
| 143 | n/a | _check_not_importing_main() |
|---|
| 144 | n/a | d = dict( |
|---|
| 145 | n/a | log_to_stderr=util._log_to_stderr, |
|---|
| 146 | n/a | authkey=process.current_process().authkey, |
|---|
| 147 | n/a | ) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | if util._logger is not None: |
|---|
| 150 | n/a | d['log_level'] = util._logger.getEffectiveLevel() |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | sys_path=sys.path.copy() |
|---|
| 153 | n/a | try: |
|---|
| 154 | n/a | i = sys_path.index('') |
|---|
| 155 | n/a | except ValueError: |
|---|
| 156 | n/a | pass |
|---|
| 157 | n/a | else: |
|---|
| 158 | n/a | sys_path[i] = process.ORIGINAL_DIR |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | d.update( |
|---|
| 161 | n/a | name=name, |
|---|
| 162 | n/a | sys_path=sys_path, |
|---|
| 163 | n/a | sys_argv=sys.argv, |
|---|
| 164 | n/a | orig_dir=process.ORIGINAL_DIR, |
|---|
| 165 | n/a | dir=os.getcwd(), |
|---|
| 166 | n/a | start_method=get_start_method(), |
|---|
| 167 | n/a | ) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | # Figure out whether to initialise main in the subprocess as a module |
|---|
| 170 | n/a | # or through direct execution (or to leave it alone entirely) |
|---|
| 171 | n/a | main_module = sys.modules['__main__'] |
|---|
| 172 | n/a | main_mod_name = getattr(main_module.__spec__, "name", None) |
|---|
| 173 | n/a | if main_mod_name is not None: |
|---|
| 174 | n/a | d['init_main_from_name'] = main_mod_name |
|---|
| 175 | n/a | elif sys.platform != 'win32' or (not WINEXE and not WINSERVICE): |
|---|
| 176 | n/a | main_path = getattr(main_module, '__file__', None) |
|---|
| 177 | n/a | if main_path is not None: |
|---|
| 178 | n/a | if (not os.path.isabs(main_path) and |
|---|
| 179 | n/a | process.ORIGINAL_DIR is not None): |
|---|
| 180 | n/a | main_path = os.path.join(process.ORIGINAL_DIR, main_path) |
|---|
| 181 | n/a | d['init_main_from_path'] = os.path.normpath(main_path) |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | return d |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | # |
|---|
| 186 | n/a | # Prepare current process |
|---|
| 187 | n/a | # |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | old_main_modules = [] |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def prepare(data): |
|---|
| 192 | n/a | ''' |
|---|
| 193 | n/a | Try to get current process ready to unpickle process object |
|---|
| 194 | n/a | ''' |
|---|
| 195 | n/a | if 'name' in data: |
|---|
| 196 | n/a | process.current_process().name = data['name'] |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | if 'authkey' in data: |
|---|
| 199 | n/a | process.current_process().authkey = data['authkey'] |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | if 'log_to_stderr' in data and data['log_to_stderr']: |
|---|
| 202 | n/a | util.log_to_stderr() |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | if 'log_level' in data: |
|---|
| 205 | n/a | util.get_logger().setLevel(data['log_level']) |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | if 'sys_path' in data: |
|---|
| 208 | n/a | sys.path = data['sys_path'] |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | if 'sys_argv' in data: |
|---|
| 211 | n/a | sys.argv = data['sys_argv'] |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | if 'dir' in data: |
|---|
| 214 | n/a | os.chdir(data['dir']) |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | if 'orig_dir' in data: |
|---|
| 217 | n/a | process.ORIGINAL_DIR = data['orig_dir'] |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | if 'start_method' in data: |
|---|
| 220 | n/a | set_start_method(data['start_method'], force=True) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | if 'init_main_from_name' in data: |
|---|
| 223 | n/a | _fixup_main_from_name(data['init_main_from_name']) |
|---|
| 224 | n/a | elif 'init_main_from_path' in data: |
|---|
| 225 | n/a | _fixup_main_from_path(data['init_main_from_path']) |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | # Multiprocessing module helpers to fix up the main module in |
|---|
| 228 | n/a | # spawned subprocesses |
|---|
| 229 | n/a | def _fixup_main_from_name(mod_name): |
|---|
| 230 | n/a | # __main__.py files for packages, directories, zip archives, etc, run |
|---|
| 231 | n/a | # their "main only" code unconditionally, so we don't even try to |
|---|
| 232 | n/a | # populate anything in __main__, nor do we make any changes to |
|---|
| 233 | n/a | # __main__ attributes |
|---|
| 234 | n/a | current_main = sys.modules['__main__'] |
|---|
| 235 | n/a | if mod_name == "__main__" or mod_name.endswith(".__main__"): |
|---|
| 236 | n/a | return |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | # If this process was forked, __main__ may already be populated |
|---|
| 239 | n/a | if getattr(current_main.__spec__, "name", None) == mod_name: |
|---|
| 240 | n/a | return |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | # Otherwise, __main__ may contain some non-main code where we need to |
|---|
| 243 | n/a | # support unpickling it properly. We rerun it as __mp_main__ and make |
|---|
| 244 | n/a | # the normal __main__ an alias to that |
|---|
| 245 | n/a | old_main_modules.append(current_main) |
|---|
| 246 | n/a | main_module = types.ModuleType("__mp_main__") |
|---|
| 247 | n/a | main_content = runpy.run_module(mod_name, |
|---|
| 248 | n/a | run_name="__mp_main__", |
|---|
| 249 | n/a | alter_sys=True) |
|---|
| 250 | n/a | main_module.__dict__.update(main_content) |
|---|
| 251 | n/a | sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | def _fixup_main_from_path(main_path): |
|---|
| 255 | n/a | # If this process was forked, __main__ may already be populated |
|---|
| 256 | n/a | current_main = sys.modules['__main__'] |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | # Unfortunately, the main ipython launch script historically had no |
|---|
| 259 | n/a | # "if __name__ == '__main__'" guard, so we work around that |
|---|
| 260 | n/a | # by treating it like a __main__.py file |
|---|
| 261 | n/a | # See https://github.com/ipython/ipython/issues/4698 |
|---|
| 262 | n/a | main_name = os.path.splitext(os.path.basename(main_path))[0] |
|---|
| 263 | n/a | if main_name == 'ipython': |
|---|
| 264 | n/a | return |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | # Otherwise, if __file__ already has the setting we expect, |
|---|
| 267 | n/a | # there's nothing more to do |
|---|
| 268 | n/a | if getattr(current_main, '__file__', None) == main_path: |
|---|
| 269 | n/a | return |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | # If the parent process has sent a path through rather than a module |
|---|
| 272 | n/a | # name we assume it is an executable script that may contain |
|---|
| 273 | n/a | # non-main code that needs to be executed |
|---|
| 274 | n/a | old_main_modules.append(current_main) |
|---|
| 275 | n/a | main_module = types.ModuleType("__mp_main__") |
|---|
| 276 | n/a | main_content = runpy.run_path(main_path, |
|---|
| 277 | n/a | run_name="__mp_main__") |
|---|
| 278 | n/a | main_module.__dict__.update(main_content) |
|---|
| 279 | n/a | sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | def import_main_path(main_path): |
|---|
| 283 | n/a | ''' |
|---|
| 284 | n/a | Set sys.modules['__main__'] to module at main_path |
|---|
| 285 | n/a | ''' |
|---|
| 286 | n/a | _fixup_main_from_path(main_path) |
|---|