title: Periodic Python Threads with on demand trigger
body: <p>I have simple PyGTK app. Since I have to run multiple periodic tasks to fetch some data and refresh GUI, I extended Thread like this:</p>
  
  <pre><code>class MyThread(threading.Thread):        
      def __init__(self):
          threading.Thread.__init__(self)
          self.setDaemon(True)
          self.event = threading.Event()
          self.event.set()
  
      def run(self):
          while self.event.is_set():
              timer = threading.Timer(60, self._run)   
              timer.start()
              timer.join()
  
      def cancel(self):
          self.event.clear()
  
      def _run(self):
          gtk.threads_enter()
          # do what need to be done, fetch data, update GUI
          gtk.threads_leave()
  </code></pre>
  
  <p>I start threads on app bootstrap, save them in some list and cancel them before exit. This works just perfect. </p>
  
  <p>But now I want to add refresh button which will force one of the threads to run immediately and not wait period of time to be run, if not currently running.</p>
  
  <p>I tried to do that by adding bool var to MyThread to indicate whether a thread is running or not (set before _run, reset on complete), and then just call MyThread._run() if not running, but that causes my app to become unresponsive and _run task to never finish execution.</p>
  
  <p>I'm not sure why this happens. What is the best way to solve this problem? It would be also fine if I can make refresh running in background so it does not block GUI.</p>
  
  <p>Maybe to call run and pass in number of seconds to 1 so timer can trigger it sooner?</p>