import os import time from concurrent.futures.process import ProcessPoolExecutor from multiprocessing import Manager from multiprocessing.managers import BaseManager
import os import time from concurrent.futures.process import ProcessPoolExecutor from multiprocessing import Manager from multiprocessing.managers import BaseManager
The Proxy objects used by multiprocessing.BaseManager and its sub-classes normally only expose methods from the objects they’re referring to, not attributes. Now, there is multiprocessing.Manager().Namespace, which provides a Proxy sub-class that does provide access to attributes, rather than methods. We can create our own Proxy type which inherits from that, which enables access to all our attributes, as well as access to our b function
from concurrent.futures.process import ProcessPoolExecutor from multiprocessing.managers import BaseManager
classA: pass
classT: def__init__(self, c): self.test = None self.c = c self.proc()
defproc(self): self.test = A()
defget_a(self): print(self.test)
defget_c(self): print(self.c)
if __name__ == '__main__': b = BaseManager() b.register("T", T) b.start() t = b.T("123") process_pools = ProcessPoolExecutor(max_workers=4) for i in range(10): process_pools.submit(t.get_c)
defproc(t, l): if l.acquire(): t.proc() t.get_t() l.release()
if __name__ == '__main__': b = BaseManager() b.register("T", T) b.register("B", B) b.start() t = b.T() b = b.B() l = Manager().Lock()
process_pools = ProcessPoolExecutor(max_workers=4) for i in range(200): if i % 2 == 0: process_pools.submit(proc, t, l) else: process_pools.submit(proc, b, l)
defproc(t, l): if l.acquire(): t.proc() t.get_t() l.release()
if __name__ == '__main__': b = BaseManager() b.register("T", T) b.register("B", B) b.start() t = b.T() b = b.B() l = Manager().Lock()
process_pools = ProcessPoolExecutor(max_workers=4) for i in range(200): if i % 2 == 0: process_pools.submit(proc, t, l) else: process_pools.submit(proc, b, l)