我对 pyudev 库的使用有疑问。
我想要一个程序来检测 USB 插入并打印一些东西到控制台。这是我的代码:
import glib
import os
import sys
from pyudev import Context, Monitor
from pyudev.glib import GUDevMonitorObserver as MonitorObserver
def device_event(observer, device):
print 'yep'
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
observer = MonitorObserver(monitor)
observer.connect('device-added', device_event)
monitor.start()
glib.MainLoop().run()
问题是当我运行脚本时,似乎 device_event 函数被调用了两次。我插入 USB 设备时得到的输出是:
yep
yep
找遍了,没找到答案,求帮助,谢谢