Mar-12-2023, 08:54 PM
In the snippet below I demonstrate how passing an instance method to ProcessPoolExecutor's initializer argument results in the assignment being 'lost' in the spawned process.
Is this by design?
Is this by design?
from concurrent.futures import ProcessPoolExecutor, wait
from os import getpid
class Handler:
def initialize(self):
print(f"initialize {getpid()}")
self.test = "one"
def handle(self):
try:
test = self.test
except AttributeError:
print(f"handle {getpid()} error")
else:
print(f"handle {getpid()} {test}")
handler = Handler()
# this works, presumably because "one" is pickled before process is forked
#handler.initialize()
print('start')
with ProcessPoolExecutor(max_workers=4, initializer=handler.initialize) as ex:
jobs = [ex.submit(handler.handle) for n in range(4)]
wait(jobs)
print('done')
