使用 python select.kqueue() 检查文件是否被修改、删除或扩展

发布于 2024-12-13 11:16:51 字数 629 浏览 12 评论 0原文

您好,我很难理解如何使用仅限 BSD 的 python 模块类 select.kqueue 和 select.kevent 来设置文件写入事件的监视。

我希望 python 程序能够在另一个进程写入文本文件时做出响应。 我的测试代码如下:

    import os
    myfd = os.open("/Users/hari/c2cbio/t.txt",os.O_RDONLY)
    my_event=select.kevent(myfd,filter=select.KQ_FILTER_VNODE,fflags=select.KQ_NOTE_WRITE|select.KQ_NOTE_EXTEND)

    # I now create a kqueue object and a control object

    kq = select.kqueue()
    # I dont know how to set the max_events if it is non zero the REPL does not return
    kq.control([my_event],0,None)

我不知道如何继续检查这些事件是否确实发生。有人可以向我指出使用 kqueue 检测文件修改或任何其他事件(如文件删除、文件重命名等)的示例吗

Hi I am having a hard time understanding how to use the BSD only python module classes select.kqueue and select.kevent to setup a watch for file write events.

I want to a python program to respond whenever a text file is written to by another process.
My test code goes as follows:

    import os
    myfd = os.open("/Users/hari/c2cbio/t.txt",os.O_RDONLY)
    my_event=select.kevent(myfd,filter=select.KQ_FILTER_VNODE,fflags=select.KQ_NOTE_WRITE|select.KQ_NOTE_EXTEND)

    # I now create a kqueue object and a control object

    kq = select.kqueue()
    # I dont know how to set the max_events if it is non zero the REPL does not return
    kq.control([my_event],0,None)

I dont know How to proceed to check that these events have indeed happened. Can someone point me to an example of using kqueue to detect file modification or any other events ( like file delete , file rename etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-12-20 11:16:51

查看看门狗模块的代码,我想出了这个。我不知道flag是否有必要。

#/usr/bin/env python
import select
import os

kq = select.kqueue()
# Use the OSX specific os.EVTONLY
# http://code.google.com/p/python-watchdog/source/browse/src/watchdog/observers/kqueue.py
fd = os.open("/Users/hari/c2cbio/t.txt", 0x8000)

ev = [select.kevent(fd, filter=select.KQ_FILTER_VNODE,flags=select.KQ_EV_ADD | select.KQ_EV_ENABLE | select.KQ_EV_CLEAR,fflags=select.KQ_NOTE_WRITE | select.KQ_NOTE_EXTEND)]
#This call will block till the write or extend events occur
evts = kq.control(ev,1,None)
print "event occurred"

Looking at the code for the watchdog module I came up with this . I dont know if the flags are necessary.

#/usr/bin/env python
import select
import os

kq = select.kqueue()
# Use the OSX specific os.EVTONLY
# http://code.google.com/p/python-watchdog/source/browse/src/watchdog/observers/kqueue.py
fd = os.open("/Users/hari/c2cbio/t.txt", 0x8000)

ev = [select.kevent(fd, filter=select.KQ_FILTER_VNODE,flags=select.KQ_EV_ADD | select.KQ_EV_ENABLE | select.KQ_EV_CLEAR,fflags=select.KQ_NOTE_WRITE | select.KQ_NOTE_EXTEND)]
#This call will block till the write or extend events occur
evts = kq.control(ev,1,None)
print "event occurred"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文