title: flask application with watchdog observer
body: <p>I am seeking for an example for flask-based web application with watchdog observer. More specifically, I want to use the watchdog observer to detect any changes in pre-defined directories and update a web application based on the changes. I can find many examples for each of them, i.e. flask-based web applications and watchdog observer examples. </p>
  
  <p>But, I don't know how to integrate two examples and run them smoothly. Can anyone provide a simple example?</p>
  
  <p>Also, I wonder if I can run the watchdog observer with Celery worker?</p>
  
  <p>Thanks</p>
  
  <p>EDIT:
  I used a celery worker to run the watchdog observer to watch a directory and its subdirectories as follows:</p>
  
  <pre><code>@celery.task(bind=True)
  def _watcher(self):
     observer = Observer()
     handler = MyHandler()
     observer.schedule(handler, '.')
     observer.start()
     try:
        while True:
           if not handler.event_q.empty():
              event, ts = handler.event_q.get()
              self.update_state(state='PROGRESS', meta={'src_path': event.src_path, 'event_type': event.event_type})
              time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
    return {'src_path': 'srcpath', 'event_type': 'eventtype'}
  </code></pre>
  
  <p>Then, from the front-end side, every 1 second, it called GET function to update any changes, if there are. This is a bit hacky.</p>
  
  <p>What I eventually want to achieve is that 1) keep watching a directory and its subdirectories, 2) if there are any changes, update a database according to the changes and 3) update front-end side based on the changes. </p>
  
  <p>So far, I could update a database based the changes in the filesystem using watchdog (MyHandler class in the above code). But, I am still seeking for a better solution to observe the changes within a flask framework and to update the changes in the front-end side.  </p>