0

I was using kqueue in Python 2.7 to build a file monitor.

Initially, it kept outputting 0x4000 in flags and 0x1 in data, which turns out to be an error occurred. Then I found one example given by LaclefYoshi, and it works!

My code, giving errors.

import select
from time import sleep

fd = open('test').fileno()
kq = select.kqueue()

flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE | select.KQ_EV_CLEAR
fflags = select.KQ_NOTE_DELETE | select.KQ_NOTE_WRITE | select.KQ_NOTE_EXTEND \
         | select.KQ_NOTE_RENAME | select.KQ_NOTE_REVOKE | select.KQ_NOTE_ATTRIB\
         | select.KQ_NOTE_LINK
ev = select.kevent(fd, filter=select.KQ_FILTER_VNODE,
                   flags=flags, fflags=fflags)


evl = kq.control([ev], 1)
print evl
while 1:
    revents = kq.control([], 1, None)
    print revents
    sleep(1)

His version, give the file object directly to the kevent function.

fd = open('test')
ev = select.kevent(fd, filter=select.KQ_FILTER_VNODE,
                   flags=flags, fflags=fflags)

Another version, call a fileno method in kevent.

fd = open('test')
ev = select.kevent(fd.fileno(), filter=select.KQ_FILTER_VNODE,
                   flags=flags, fflags=fflags)

But now I'm really confused of why the first version doesn't work while the third one works well. These two should be the same thing, right?

The other question I have is, what exactly is a file object here in Python? I've seen the ident is actually an integer here, which should be the file descriptor instead of file objects. How does it work here!?

Thanks!

xh4n3
  • 1
  • 1
  • What exactly does "Doesn't work mean" – e4c5 Sep 06 '15 at 06:05
  • @e4c5 kept generating flags=0x4000 and data=0x1, an error cannot be found on man page. – xh4n3 Sep 06 '15 at 06:15
  • Maybe it is because you dont have any references to file object and garbage collector closes it? – dizballanze Sep 06 '15 at 09:14
  • @dizballanze You might be right, but after I called the gc.disable(), the error is still there. Any other method to test if it was because the garbage collector? Thanks! – xh4n3 Sep 06 '15 at 10:28
  • @xh4n3 try to keep reference to the file object in variable – dizballanze Sep 06 '15 at 11:23
  • @dizballanze Yeah, it does work if I have a variable referring to this file. But disabling the garbage collector cannot stop the error, so I cant simply say gc is the reason for that. – xh4n3 Sep 07 '15 at 06:08

0 Answers0