1

I have problems to catch errors inside an event handler from pyinotify. I am trying to do some processing on files that have just been closed after being written to.

Here is a simplified version of my script:

import pyinotify
import asyncore

mask = pyinotify.IN_CLOSE_WRITE

class EventHandler(pyinotify.ProcessEvent):

    def process_IN_CLOSE_WRITE(self, event):
        try:
            do_stuff()
        except BaseException as e:
            print "Exception:" + str(e)
            pass

if __name__ == "__main__":
        try:
                wm = pyinotify.WatchManager()

                notifier = pyinotify.AsyncNotifier(wm, EventHandler())
                wdd = wm.add_watch('/dir-to-watch/', mask, rec=True)
                asyncore.loop()
        except:
                print "Unhandled error!"
                print "Details:" + str(e)
                print "Continuing anyway..."
                pass

It seems that when I get an error or an exception neither my except in the main loop or my except BaseException in the event handler are catching the error or exceptions.

I get messages beginning like this:

error: uncaptured python exception, closing channel (:[Errno 2] No such file or directory:

So my question is: how could catch those exceptions?

elhoim
  • 6,705
  • 2
  • 23
  • 29

1 Answers1

0

I had to create a custom AsyncNotifier:

class CustomAsyncNotifier(pyinotify.AsyncNotifier):
        def handle_error(self):
                print "Handling error!"
                print "Guru meditiation #00000025.65045338"
                print ""
                print "Continuing anyway..."

and then change my code to use it:

if __name__ == "__main__":
        wm = pyinotify.WatchManager()

        notifier = CustomAsyncNotifier(wm, EventHandler())
        wdd = wm.add_watch('/mnt/md0/proxies/', mask, rec=True)
        asyncore.loop()
elhoim
  • 6,705
  • 2
  • 23
  • 29