WTimer.py 699 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python3
  2. # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
  3. # WeCase -- Linux Sina Weibo Client
  4. # This file implemented a QTimer-like timer
  5. # Copyright (C) 2013 Tom Li
  6. # License: GPL v3 or later.
  7. from threading import Thread, Event
  8. class WTimer(Thread):
  9. def __init__(self, run_function, sleep_time):
  10. super(WTimer, self).__init__()
  11. self.sleep_time = sleep_time
  12. self.run_function = run_function
  13. self._stop_event = Event()
  14. def run(self):
  15. while not self._stop_event.is_set():
  16. self._stop_event.wait(self.sleep_time)
  17. self.run_function()
  18. def stop(self):
  19. self._stop_event.set()
  20. self.join()