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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值