2016-07-04 33 views
1

我有問題從pyinotify中捕獲事件處理程序中的錯誤。 我正試圖對寫入後剛剛關閉的文件進行一些處理。在pyinotify中捕獲錯誤ProcessEvent

這裏是我的腳本的簡化版本:

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 

看來,當我在事件處理程序錯誤或異常既不是我在主迴路除了或我除了BaseException正在捕捉錯誤或異常。

我開始得到這樣的消息:

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

所以我的問題是:如何能抓住這些例外?

回答

0

我不得不創建一個自定義AsyncNotifier:

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

,然後改變我的代碼使用它:

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

     notifier = CustomAsyncNotifier(wm, EventHandler()) 
     wdd = wm.add_watch('/mnt/md0/proxies/', mask, rec=True) 
     asyncore.loop()