pyinotify用于在文件夹中查看新文件创建

时间:2015-11-18 19:00:06

标签: python watch pyinotify

我正在使用python中的pyinotify模块观看一个文件夹being_watched。但是文件的创建速度比pyinotify的事件处理程序可以处理的速度快。有没有解决这个问题的方法,所有创建的新文件可以排队或稍后由事件处理程序接收?

代码段:

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        print "Creating:", event.pathname
        #start_time=time.time()
        #reading the data 
        with open(str(event.pathname)) as ofile:
            d = json.loads(ofile.read())
            for _id in d:
                if d[_id]["_comment"]>0:
                    inputdata.update({_id:{"la":d[message_id]["la"],"lo":d[_id]["lo"]}})


        try:
            getcdata(inputdata,outfile)
        except RuntimeError as e:
            logger.error('Caught a Rutime exception in the getcdatadriver . '+str(e.value))

def main():
    logger = logging.getLogger('mainloop')
    logger.setLevel(logging.DEBUG)
    fh = logging.FileHandler('getcdatadriver.log')
    logger.addHandler(fh)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    sys.excepthook=Notify(logger,"Exception caught getcdatadriver mainloop").my_excepthook
    try:
        while True:
                watch_dir="./output_watchdir"
                wm = pyinotify.WatchManager()  # Watch Manager
                mask = pyinotify.IN_CREATE  # watched events
                handler = EventHandler()
                notifier = pyinotify.Notifier(wm, handler)
                wdd = wm.add_watch(watch_dir, mask, rec=True)
                notifier.loop()
    except (KeyboardInterrupt, SystemExit):
        print '\n! Received keyboard interrupt, quitting.\n'
        Notify(logger,"getcdatadriver stopped").send_exception_email('Received keyboard interrupt, quitting.')
    except RuntimeError as e:
        print 'Caught a Rutime exception in the getcdatadriver mainloop. '+str(e.value)
        Notify(logger,"Runtime Exception getcdatadriver mainloop").send_exception_email(str(e.value))

0 个答案:

没有答案