thread_handling.py 713 B

12345678910111213141516171819202122232425
  1. import threading
  2. import sys
  3. class ThreadHandler(object):
  4. def __init__(self, name, callable, *args, **kwargs):
  5. # Set up exception handling
  6. self.exception = None
  7. def wrapper(*args, **kwargs):
  8. try:
  9. callable(*args, **kwargs)
  10. except BaseException:
  11. self.exception = sys.exc_info()
  12. # Kick off thread
  13. thread = threading.Thread(None, wrapper, name, args, kwargs)
  14. thread.setDaemon(True)
  15. thread.start()
  16. # Make thread available to instantiator
  17. self.thread = thread
  18. def raise_if_needed(self):
  19. if self.exception:
  20. e = self.exception
  21. raise e[0], e[1], e[2]