脚本1
from pyinotify import WatchManager, Notifier, \ ProcessEvent, IN_CREATE, IN_MODIFY, IN_DELETE, IN_CLOSE_WRITE import threading, Queue import os class mqpf_thread(threading.Thread): def __init__(self, events_queue): super(mqpf_thread, self).__init__() self.daemon = True # False self.queue = events_queue def run(self): while 1: event = self.queue.get() fp = event.path fn = event.name fpth = os.path.join(fp, fn) print fpth self.queue.task_done() class EventHandler(ProcessEvent): def __init__(self, events_queue): super(EventHandler, self).__init__() self.events_queue = events_queue def process_IN_CLOSE_WRITE(self, event): self.events_queue.put(event) path = "/home/gao/testInotify/fold" numThreads = 10 events_queue = Queue.Queue() thread_pool = list() for i in range(numThreads): thread_pool.append(mqpf_thread(events_queue)) for i in thread_pool: i.start() j = 0 for item in threading.enumerate(): print j, item j += 1 wm = WatchManager() # mask = IN_DELETE | IN_CLOSE_WRITE mask = IN_CLOSE_WRITE notifier = Notifier(wm, EventHandler(events_queue)) wm.add_watch(path, mask, rec=True) # notifier.loop() print 'now starting monitor %s' % (path) while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() except KeyboardInterrupt: notifier.stop() break