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.
But, I don't know how to integrate two examples and run them smoothly. Can anyone provide a simple example?
Also, I wonder if I can run the watchdog observer with Celery worker?
Thanks
EDIT: I used a celery worker to run the watchdog observer to watch a directory and its subdirectories as follows:
@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'}
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.
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.
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.