Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. from multiprocessing import Pool, TimeoutError
  2. import time
  3. import os
  4.  
  5. def print_for_three_seconds():
  6. for i in range(12):
  7. print(i)
  8. time.sleep(0.25)
  9.  
  10. def crash_in_one_second():
  11. time.sleep(1)
  12. raise Exception(f"Intentionally crashing from thread {os.getpid()}")
  13.  
  14. def die_completely(e):
  15. print(f"die_completely callback executed in thread {os.getpid()}")
  16. pool.terminate()
  17. raise Exception("Child process threw an exception", e)
  18.  
  19. if __name__ == '__main__':
  20. pool = Pool(processes=4) # start 4 worker processes
  21.  
  22. for _ in range(3):
  23. pool.apply_async(print_for_three_seconds, error_callback=die_completely)
  24. pool.apply_async(crash_in_one_second, error_callback=die_completely)
  25. pool.close()
  26. print(f"Main thread pid={os.getpid()} ready to join")
  27. pool.join()
  28.  
  29. print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement