I am watching a folder being_watched using the pyinotify module in python. But the files are being created faster than they can be processed by the Event Handler of the pyinotify. Is there a way around this problem, where all the new files created can be lined up or taken up later by the Event Handler?
Code snippet:
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))