关于PyGTK+pyinotify
首先是pyinotify官网上的一个例子
1 import pyinotify
2
3 wm = pyinotify.WatchManager() # Watch Manager
4 mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events
5
6 class EventHandler(pyinotify.ProcessEvent):
7 def process_IN_CREATE(self, event):
8 print "Creating:", event.pathname
9
10 def process_IN_DELETE(self, event):
11 print "Removing:", event.pathname
12
13 handler = EventHandler()
14 notifier = pyinotify.Notifier(wm, handler)
15 wdd = wm.add_watch('/tmp', mask, rec=True)
16
17 notifier.loop()
在这个例子中最后一行的notifier.loop()是inotify自己的loop实现
但如果想要PyGTK中使用inotify的话notifify的loop会和GTK的loop发生冲突
所以要做下小的修改
1 import pyinotify
2 import gobject
3 import gtk
4
5 gobject.threads_init()
6
7 wm = pyinotify.WatchManager() # Watch Manager
8 mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events
9
10 class EventHandler(pyinotify.ProcessEvent):
11 def process_IN_CREATE(self, event):
12 print "Creating:", event.pathname
13
14 def process_IN_DELETE(self, event):
15 print "Removing:", event.pathname
16
17
18 def _quit(widget):
19 notifier.stop()
20 gtk.main_quit()
21
22 handler = EventHandler()
23 notifier = pyinotify.ThreadedNotifier(wm, handler)
24 wdd = wm.add_watch('/tmp', mask, rec=True)
25 notifier.start()
26
27 win=gtk.Window()
28 win.connect("destroy",_quit)
29 win.show()
30 gtk.main()