无需轮询即可检测文件更改 [英] Detect File Change Without Polling
问题描述
我正在尝试使用 Python 程序中的方法来检测文件系统上的文件是否已被修改.我知道我可以每 5 秒运行一次以检查系统的最后修改日期,但我很好奇是否有更简单的方法来执行此操作,而无需要求我的程序重复检查.
I'm trying to use a method within a Python program to detect whether a file on the file system has been modified. I know that I could have something run on an every-5-seconds to check the last modification date off of the system, but I was curious as to whether there's an easier method for doing this, without needing to require my program to check repeatedly.
有人知道这样的方法吗?
Does anyone know of such a method?
推荐答案
对于 linux,有 pyinotify.
For linux, there is pyinotify.
来自主页:
Pyinotify 是一个 Python 模块,用于监控文件系统的变化.Pyinotify 依赖于 Linux 内核功能(在内核 2.6.13 中合并)称为 inotify.inotify 是一个事件驱动的通知程序,其通知从内核导出通过三个空间到用户空间系统调用.pyinotify 绑定这些系统调用并提供在他们提供的基础上实施一种通用和抽象的方式操纵这些功能.
Pyinotify is a Python module for monitoring filesystems changes. Pyinotify relies on a Linux Kernel feature (merged in kernel 2.6.13) called inotify. inotify is an event-driven notifier, its notifications are exported from kernel space to user space through three system calls. pyinotify binds these system calls and provides an implementation on top of them offering a generic and abstract way to manipulate those functionalities.
因此它显然不是跨平台的,并且依赖于足够新的内核版本.但是,据我所知,任何非轮询机制都需要内核支持.
Thus it is obviously not cross-platform and relies on a new enough kernel version. However, as far as I can see, requiring kernel support would be true about any non-polling mechanism.
这篇关于无需轮询即可检测文件更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!