建设网站需要哪些域名太原app开发公司
web/
2025/10/6 19:32:03/
文章来源:
建设网站需要哪些域名,太原app开发公司,网站建设需求文档下载,湛江师范学院网站开发技术起步
在python中文件监控主要有两个库#xff0c;一个是pyinotify#xff0c;一个是watchdog。pyinotify依赖于Linux平台的inotify#xff0c;后者则对不同平台的的事件都进行了封装。因为我主要用于Windows平台#xff0c;所以下面着重介绍watchdog#xff08;推荐大家阅…起步
在python中文件监控主要有两个库一个是pyinotify一个是watchdog。pyinotify依赖于Linux平台的inotify后者则对不同平台的的事件都进行了封装。因为我主要用于Windows平台所以下面着重介绍watchdog推荐大家阅读一下watchdog实现源码有利于深刻的理解其中的原理。 watchdog在不同的平台使用不同的方法进行文件检测。在init.py中发现了如下注释
|Inotify| Linux 2.6.13 inotify(7) based observer
|FSEvents| Mac OS X FSEvents based observer
|Kqueue| Mac OS X and BSD with kqueue(2) kqueue(2) based observer
|WinApi|(ReadDirectoryChangesW) MS Windows Windows API-based observer
|Polling| Any fallback implementation给出示例代码如下
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created by victor# 本模块的功能:检测文件夹变化# 导入watchdog对应模块
from watchdog.observers import Observer
from watchdog.events import *
# 导入时间模块
import timeclass FileEventHandler(FileSystemEventHandler):# 初始化魔术方法def __init__(self):FileSystemEventHandler.__init__(self)# 文件或文件夹移动def on_moved(self, event):if event.is_directory:print(directory moved from {0} to {1}.format(event.src_path,event.dest_path))else:print(file moved from {0} to {1}.format(event.src_path,event.dest_path))# 创建文件或文件夹def on_created(self, event):if event.is_directory:print(directory created:{0}.format(event.src_path))else:print(file created:{0}.format(event.src_path))# 删除文件或文件夹def on_deleted(self, event):if event.is_directory:print(directory deleted:{0}.format(event.src_path))else:print(file deleted:{0}.format(event.src_path))# 移动文件或文件夹def on_modified(self, event):if event.is_directory:print(directory modified:{0}.format(event.src_path))else:print(file modified:{0}.format(event.src_path))if __name__ __main__:# 实例化Observer对象observer Observer()event_handler FileEventHandler()# 设置监听目录dis_dir e:/observer.schedule(event_handler,dis_dir,True)observer.start()try:while True:# 设置监听频率(间隔周期时间)time.sleep(1)except KeyboardInterrupt:observer.stop()observer.join()小结
watchdog主要采用观察者模型废话从变量命名就可以看出来。主要有三个角色observerevent_handler被监控的文件夹。三者原本是独立的主要通过observer.schedule函数将三者串起来意思为observer不断检测调用平台依赖代码对监控文件夹进行变动检测当发现改变时通知event_handler处理。最后特别推荐读者有时间可以阅读一下watchdog的源码写的易懂而且架构很好用
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88077.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!