Created
December 27, 2019 06:59
-
-
Save xbklairith/f0b4d22fa505610c5f92dff5cbbd3e94 to your computer and use it in GitHub Desktop.
Create process(s) upon the number of workers, The pool will be terminated when an exception occurs in one of the workers and Kube-pod restarting is expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
from multiprocessing import Pool | |
from functools import partial | |
def _on_error(pool, e): | |
pool.terminate() | |
raise e | |
def spawn(workers): | |
""" | |
Create process(s) upon the number of workers, | |
The pool will be terminated when an exception occurs in one of the workers | |
and Kube-pod restarting is expected | |
Arguments: | |
workers {[functions]} -- list of worker function | |
""" | |
pool = Pool(len(workers)) | |
results = [ | |
pool.apply_async(worker, error_callback=partial(_on_error, pool)) | |
for worker in workers | |
] | |
pool.close() | |
pool.join() # wait for pool terminated | |
return results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment