Source

woocode / py / sa / wild / example_kqueue.py

Full commit
#!/usr/bin/env python
# -*- encoding:utf-8 -*-

# understanding how to get kqueue working in python

import select
import os
import sys

from select import kqueue, kevent

filename = 'access.log'
fd = os.open(filename, os.O_RDONLY)
kq = kqueue()

event = [kevent(fd,
                filter=select.KQ_FILTER_READ,
                flags=select.KQ_EV_ADD),
         kevent(fd,
                filter=select.KQ_FILTER_VNODE,
                flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR,
                fflags=select.KQ_NOTE_DELETE | select.KQ_NOTE_RENAME)]

events = kq.control(event, 0, 0)
while True:
    print 'Loop'
    r_events = kq.control(None, 4)
    for event in r_events:
        print event
        for event in r_events:
            if event.fflags & select.KQ_NOTE_DELETE:
                print 'file was deleted'
            elif event.fflags & select.KQ_NOTE_RENAME:
                print 'file was renamed'

kq.close()
os.close(fd)