watchdog
问题
最近面临一个问题是其他业务提交模型文件,服务Flask接口需要实时的更新到该新的模型文件。有一些常规的解决办法,比如更新git项目,打tag,jenkins自动重新拉取并build。这种可能需要业务方懂得Git的使用且需要给予他比较高的开发权限,操作不当可能引起线上事故,这里用另外的方式去解决。监控文件是否发生改变,如果发生改变就进行相应的步骤操作。虽然Flask中有Werkzeug内置的stat重载器,但是其缺点是耗电较严重且准确性一般。因此可以使用其他的监测包。这里使用Python库watchdog,安装之后就能使用它自动监测文件的变动。watchdog 是一个实时监控库,其原理是通过操作系统的时间触发,需要循环等待。
- 项目地址:https://github.com/gorakhargosh/watchdog
- 项目文档:https://python-watchdog.readthedocs.io/en/stable/index.html
安装
代码语言:javascript复制pip install watchdog
也可以通过源码安装,可以参考项目文档
例子
下面的示例程序将 递归(recursive=True) 地监视文件系统更改的工作目录,并将它们简单地记录到控制台:
代码语言:javascript复制import sys
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.' # 监控文件夹或者文件
event_handler = LoggingEventHandler()
observer = Observer() # 创建一个观察者对象
observer.schedule(event_handler, path, recursive=True) # 声明一个定时任务
observer.start() # 启动定时任务
try:
while observer.isAlive():
observer.join(1)
finally:
observer.stop()
observer.join()
Control C 停止监控
假设上面程序文件是watch.py
, 想要监控文件或者文件夹都可以,后续接文件名或者文件夹名
执行:
代码语言:javascript复制python watch.py hello
然后对hello
文件夹中的hello.py
进行更改,在该页面就能收到对应的提示信息:
2022-11-09 23:48:08 - Created file: hello/.hello.py.swp
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Created file: hello/.hello.py.swx
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Deleted file: hello/.hello.py.swx
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Deleted file: hello/.hello.py.swp
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Created file: hello/.hello.py.swp
2022-11-09 23:48:08 - Modified directory: hello
2022-11-09 23:48:08 - Modified file: hello/.hello.py.swp
2022-11-09 23:48:10 - Modified file: hello/.hello.py.swp
2022-11-09 23:48:11 - Created file: hello/4913
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Modified file: hello/4913
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Deleted file: hello/4913
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Moved file: from hello/hello.py to hello/hello.py~
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Created file: hello/hello.py
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Modified file: hello/hello.py
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Modified file: hello/hello.py
2022-11-09 23:48:11 - Modified file: hello/.hello.py.swp
2022-11-09 23:48:11 - Deleted file: hello/hello.py~
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Modified directory: hello
2022-11-09 23:48:11 - Deleted file: hello/.hello.py.swp
2022-11-09 23:48:11 - Modified directory: hello
说明进行了相应的修改,基于此信息可以做后续的逻辑操作等。总的来说,这个库还是比较方便的,不用自己去写shell脚本去定时监控文件是否发生了改变。
参考
- https://python-watchdog.readthedocs.io/en/stable/index.html
- https://www.cnblogs.com/tjp40922/p/14131147.html