◐ Shell
clean mode source ↗

Message 183191 - Python tracker

I think this change will potentially make the main module get imported twice under different names when we transfer pickled data between processes.  The current code (which is rather a mess) goes out of its way to avoid that.

Basically the main process makes sys.modules['__mp_main__'] an alias for the main module, and other process import the parent's main module with __name__ == '__mp_main__' and make sys.modules['__main__'] an alias for that.  This means that any functions/classes defined in the main module (from whatever process) will have

    obj.__module__ in {'__main__', '__mp_main__'}

Unpickling such an object will succeed in any process without reimporting the main module.

Attached is an alternative patch which is more like the original code and seems to work.  (Maybe modifying loader.name is an abuse of the API.)