Skip to content

Instantly share code, notes, and snippets.

@xbklairith
Created December 27, 2019 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xbklairith/f0b4d22fa505610c5f92dff5cbbd3e94 to your computer and use it in GitHub Desktop.
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
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