thread.py 785 B

123456789101112131415161718192021222324252627282930313233
  1. """Base class for threads."""
  2. from threading import Thread
  3. from tornado.ioloop import IOLoop
  4. CONTROL_THREAD_NAME = "Control"
  5. SHELL_CHANNEL_THREAD_NAME = "Shell channel"
  6. class BaseThread(Thread):
  7. """Base class for threads."""
  8. def __init__(self, **kwargs):
  9. """Initialize the thread."""
  10. super().__init__(**kwargs)
  11. self.io_loop = IOLoop(make_current=False)
  12. self.pydev_do_not_trace = True
  13. self.is_pydev_daemon_thread = True
  14. def run(self) -> None:
  15. """Run the thread."""
  16. try:
  17. self.io_loop.start()
  18. finally:
  19. self.io_loop.close()
  20. def stop(self) -> None:
  21. """Stop the thread.
  22. This method is threadsafe.
  23. """
  24. self.io_loop.add_callback(self.io_loop.stop)