solve bug in custom pool multiprocessing

This commit is contained in:
Noe Brucy
2020-12-14 09:51:29 +01:00
parent e0ce6729af
commit 653e64d782
3 changed files with 38 additions and 73 deletions
+3 -35
View File
@@ -12,6 +12,9 @@ from random import randint
class NoDaemonProcess(multiprocessing.Process):
def __init__(self, *args, **kwargs):
super(NoDaemonProcess, self).__init__(**kwargs)
# make 'daemon' attribute always return False
def _get_daemon(self):
return False
@@ -26,38 +29,3 @@ class NoDaemonProcess(multiprocessing.Process):
# because the latter is only a wrapper function, not a proper class.
class MyPool(multiprocessing.pool.Pool):
Process = NoDaemonProcess
def sleepawhile(t):
print("Sleeping %i seconds..." % t)
time.sleep(t)
return t
def work(num_procs):
print("Creating %i (daemon) workers and jobs in child." % num_procs)
pool = multiprocessing.Pool(num_procs)
result = pool.map(sleepawhile, [randint(1, 5) for x in range(num_procs)])
# The following is not really needed, since the (daemon) workers of the
# child's pool are killed when the child is terminated, but it's good
# practice to cleanup after ourselves anyway.
pool.close()
pool.join()
return result
def test():
print("Creating 5 (non-daemon) workers and jobs in main process.")
pool = MyPool(5)
result = pool.map(work, [randint(1, 5) for x in range(5)])
pool.close()
pool.join()
print(result)
if __name__ == "__main__":
test()