我正在使用 python 中的 pyinotify 模块观看正在观看的文件夹。但是文件的创建速度比 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))