thread_manager.py 559 B

1234567891011121314151617181920212223
  1. import threading
  2. class ThreadManager:
  3. """
  4. Manages multiple threads used to execute given handler tasks.
  5. """
  6. def __init__(self, handlers):
  7. self.handlers = handlers
  8. self.threads = []
  9. def start(self):
  10. for handler in self.handlers:
  11. thread = threading.Thread(target=handler.run)
  12. self.threads.append(thread)
  13. thread.start()
  14. def stop(self):
  15. for handler in self.handlers:
  16. handler.stop_event.set()
  17. for thread in self.threads:
  18. thread.join()