使用python select.kqueue()检查文件是否被修改删除或扩展 [英] Check if file is modified deleted or extended using python select.kqueue()

查看:216
本文介绍了使用python select.kqueue()检查文件是否被修改删除或扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想让python程序在其他程序写入文本文件时做出响应. 我的测试代码如下:

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)

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

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)

推荐答案

看一下看门狗模块的代码,我想到了这个.我不知道这些标志是否必要.

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"

这篇关于使用python select.kqueue()检查文件是否被修改删除或扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆