Inotify测试(1):PyInotify、Thread测试

脚本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



阅读更多
文章标签: pyinotify
个人分类: Python
想对作者说点什么? 我来说一句

<em>pyinotify</em>-0.9.6

2018年05月09日 0B 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭