[issue37292] _xxsubinterpreters: Can't unpickle objects defined in __main__

Lewis Gaul report at bugs.python.org
Sun Nov 24 08:18:20 EST 2019


Lewis Gaul <lewis.gaul at gmail.com> added the comment:

Just to move the conversation from the subinterpreters project repo to here...

I'm going to take a look at how this is done by subprocess using the example provided by Guido:

import os
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import get_context

class C:
    def __getstate__(self):
        print("pickled in %d" % os.getpid())
        return {}

    def __setstate__(self, state):
        print("unpickled in %d" % os.getpid())

    def hello(self):
        print("Hello world")


if __name__ == "__main__":
    with ProcessPoolExecutor(mp_context=get_context("spawn")) as ex:
        ex.submit(C().hello).result()

Output:

pickled in 23480
unpickled in 23485
Hello world

----------
nosy: +LewisGaul

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37292>
_______________________________________


More information about the Python-bugs-list mailing list