Pyudev - calls function twice

2019-01-29 10:37发布

问题:

I have a problem with pyudev library usage.

I want to have a program which detects USB plug in and prints something to console. Here's the code i have:

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()

The problem is when i run the script it seems that device_event function gets called twice. The output i get when i plug in an USB device is:

yep
yep

I searched all over but could not find an answer Please help Thank you

回答1:

You get an event for the device enumeration and then separate events for each enumerated interface.