watchdog介绍

本文介绍了如何使用Python的Watchdog库来替代Flask的Werkzeug重载器,实现实时监控文件系统变化。当模型文件更新时,避免了依赖Git和jenkins自动部署可能导致的问题。通过示例代码展示了如何设置Watchdog监听指定目录,并在文件发生变化时记录到控制台,从而可以据此执行相应的逻辑操作。Watchdog库通过操作系统事件触发,能有效节省资源并提高监控准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

watchdog

问题

最近面临一个问题是其他业务提交模型文件,服务Flask接口需要实时的更新到该新的模型文件。有一些常规的解决办法,比如更新git项目,打tag,jenkins自动重新拉取并build。这种可能需要业务方懂得Git的使用且需要给予他比较高的开发权限,操作不当可能引起线上事故,这里用另外的方式去解决。监控文件是否发生改变,如果发生改变就进行相应的步骤操作。虽然Flask中有Werkzeug内置的stat重载器,但是其缺点是耗电较严重且准确性一般。因此可以使用其他的监测包。这里使用Python库watchdog,安装之后就能使用它自动监测文件的变动。watchdog 是一个实时监控库,其原理是通过操作系统的时间触发,需要循环等待。

安装

pip install watchdog

也可以通过源码安装,可以参考项目文档

例子

下面的示例程序将 递归(recursive=True) 地监视文件系统更改的工作目录,并将它们简单地记录到控制台:

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, 想要监控文件或者文件夹都可以,后续接文件名或者文件夹名

执行:

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脚本去定时监控文件是否发生了改变。

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值