Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Pool, TimeoutError
- import time
- import os
- def print_for_three_seconds():
- for i in range(12):
- print(i)
- time.sleep(0.25)
- def crash_in_one_second():
- time.sleep(1)
- raise Exception(f"Intentionally crashing from thread {os.getpid()}")
- def die_completely(e):
- print(f"die_completely callback executed in thread {os.getpid()}")
- pool.terminate()
- raise Exception("Child process threw an exception", e)
- if __name__ == '__main__':
- pool = Pool(processes=4) # start 4 worker processes
- for _ in range(3):
- pool.apply_async(print_for_three_seconds, error_callback=die_completely)
- pool.apply_async(crash_in_one_second, error_callback=die_completely)
- pool.close()
- print(f"Main thread pid={os.getpid()} ready to join")
- pool.join()
- print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement