【问题标题】:Catch errors in pyinotify ProcessEvent在 pyinotify ProcessEvent 中捕获错误
【发布时间】:2016-07-04 11:48:01
【问题描述】:

我在从 pyinotify 捕获事件处理程序中的错误时遇到问题。 我正在尝试对写入后刚刚关闭的文件进行一些处理。

这是我的脚本的简化版本:

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

似乎当我收到错误或异常时,主循环中的 except 或事件处理程序中的 except BaseException 都没有捕获错误或异常。

我收到这样开头的消息:

错误:未捕获的 python 异常,关闭通道 (:[Errno 2] 没有这样的文件或目录:

所以我的问题是:如何捕获这些异常?

【问题讨论】:

    标签: python pyinotify


    【解决方案1】:

    我必须创建一个自定义 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()
    

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多