| 248 | # Multiprocessing module helpers to fix up the main module in |
| 249 | # spawned subprocesses |
| 250 | def _fixup_main_from_name(mod_name): |
| 251 | # __main__.py files for packages, directories, zip archives, etc, run |
| 252 | # their "main only" code unconditionally, so we don't even try to |
| 253 | # populate anything in __main__, nor do we make any changes to |
| 254 | # __main__ attributes |
| 255 | current_main = sys.modules['__main__'] |
| 256 | if mod_name == "__main__" or mod_name.endswith(".__main__"): |
| 257 | return |
| 258 | |
| 259 | # If this process was forked, __main__ may already be populated |
| 260 | if getattr(current_main.__spec__, "name", None) == mod_name: |
| 261 | return |
| 262 | |
| 263 | # Otherwise, __main__ may contain some non-main code where we need to |
| 264 | # support unpickling it properly. We rerun it as __mp_main__ and make |
| 265 | # the normal __main__ an alias to that |
| 266 | old_main_modules.append(current_main) |
| 267 | main_module = types.ModuleType("__mp_main__") |
| 268 | main_content = runpy.run_module(mod_name, |
| 269 | run_name="__mp_main__", |
| 270 | alter_sys=True) |
| 271 | main_module.__dict__.update(main_content) |
| 272 | sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module |
| 273 | |
| 274 | |
| 275 | def _fixup_main_from_path(main_path): |