带看门狗观察器的烧瓶应用 [英] flask application with watchdog observer
问题描述
我正在寻找带有看门狗观察器的基于烧瓶的Web应用程序的示例.更具体地说,我想使用看门狗观察器来检测预定义目录中的任何更改,并根据这些更改来更新Web应用程序.我可以找到许多针对每个示例的示例,即基于烧瓶的Web应用程序和看门狗观察器示例.
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?
另外,我想知道我是否可以与Celery工人一起运行看门狗观察器?
Also, I wonder if I can run the watchdog observer with Celery worker?
谢谢
我使用了一个芹菜工作者来运行看门狗观察器来观察目录及其子目录,如下所示:
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'}
然后,从前端侧每隔1秒钟调用一次GET函数,以更新所有更改(如果有).这有点hacky.
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.
我最终要实现的目标是:1)继续观察目录及其子目录,2)如果有任何更改,请根据更改来更新数据库,以及3)根据更改来更新前端.
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.
到目前为止,我可以使用看门狗(上面的代码中的MyHandler类)基于文件系统中的更改来更新数据库.但是,我仍在寻找更好的解决方案,以观察Flask框架内的更改并更新前端方面的更改.
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.
推荐答案
我最终使用了多线程.
from flask import Flask, render_template, jsonify
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from queue import Queue
import time
import threading
class MyHandler(FileSystemEventHandler):
def __init__(self, pattern=None):
self.pattern = pattern or (".xml", ".tiff", ".jpg")
self.event_q = Queue()
self.dummyThread = None
def on_any_event(self, event):
if not event.is_directory and event.src_path.endswith(self.pattern):
self.event_q.put((event, time.time()))
def start(self):
self.dummyThread = threading.Thread(target=self._process)
self.dummyThread.daemon = True
self.dummyThread.start()
def _process(self):
while True:
time.sleep(1)
app = Flask(__name__)
handler = MyHandler()
handler.start()
eventlist_flag = 0
eventlist = []
def run_watcher():
global eventlist_flag, eventlist
observer = Observer()
observer.schedule(handler, '.')
observer.start()
try:
while True:
if eventlist_flag == 0:
eventlist_flag = 1
while not handler.event_q.empty():
event, ts = handler.event_q.get()
eventlist.append(event)
eventlist_flag = 0
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
@app.route('/watcher/status', methods=['POST'])
def watchernow():
global eventlist_flag, eventlist
if eventlist_flag == 0 and len(eventlist) > 0:
eventlist_flag = 2
for e in eventlist:
print(e)
eventlist = []
eventlist_flag = 0
return jsonify({})
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
watcher_thread = threading.Thread(target=run_watcher)
watcher_thread.start()
app.run(debug=True)
watcher_thread.join()
这篇关于带看门狗观察器的烧瓶应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!