[Tutor] Directory notification
Hugo González Monteverde
hugonz-lists at h-lab.net
Thu Jun 30 16:06:49 CEST 2005
Hi all,
I was writing a daemon to continuously check for changes in a directory
and then process the new files, I was polling every X seconds, but I
decided I should give dnotify a try in Python. Here is my test program,
I just want to share it with others on the list, as it is a very nice
way to watch a directory as polling is ugly (it is Linux specific, though)
==========
#!/usr/bin/env python
# This checks the implementation for dnotify, uses fcntl and signals
import signal
from fcntl import *
import os
#set event handler for SIGIO, otherwise the program will terminate
signal.signal(signal.SIGIO, lambda signum, stack:None)
# Configure fcntl for a directory
# wanted: DN_MODIFY DN_CREATE DN_ATTRIB DN_DELETE DN_RENAME DN_MULTISHOT
dnot_dir = "/var/data/in"
# builtin open() will refuse to open a directory(!)
filep = os.open(dnot_dir, os.O_RDONLY)
while True:
# register the service
fcntl(filep, F_NOTIFY,
DN_MODIFY|DN_CREATE|DN_ATTRIB|DN_DELETE|DN_RENAME)
# block until signal is received
signal.pause()
print "EVENT!"
=================
More information about the Tutor
mailing list