前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >watchdog介绍

watchdog介绍

作者头像
用户3578099
发布2023-09-01 09:22:51
1480
发布2023-09-01 09:22:51
举报
文章被收录于专栏:AI科技时讯

问题

最近面临一个问题是其他业务提交模型文件,服务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进行更改,在该页面就能收到对应的提示信息:

代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI科技时讯 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题
  • 安装
  • 例子
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档