代码之家  ›  专栏  ›  技术社区  ›  user4992124

DBus信号接收器从不被调用

  •  0
  • user4992124  · 技术社区  · 4 年前

    在我的代码中,我注册了在插入USB设备时接收信号。

    def registerSignals(self):
        for signal, callback, bus, interface in (('InterfacesAdded', self.onUSBInterfaceAdded, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager'),
                                                ('InterfacesRemoved', self.onUSBInterfaceRemoved, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager'),
                                                ('PropertiesChanged', self.onUSBMountPropertiesChanged, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager')):
            print(self.__bus.add_signal_receiver(callback,
                                            bus_name=bus,
                                            signal_name=signal,
                                            dbus_interface=interface))
                                           
    def onUSBInterfaceAdded(self, path, interfaces):
        print(path)
        print(interfaces)
    
    def onUSBInterfaceRemoved(self, path, interfaces):
        print(path)
        print(interfaces)
    
    def onUSBMountPropertiesChanged(self, interface, properties, invalidated, path):
        print(path)
        print(interfaces)
    

    现在,当我运行这段代码时,我得到 信号。问题是,当我跑步时 dbus-monitor --system 在CLI中,插入USB设备时确实会看到消息。知道如何调试这个吗?或者我在这里做错了什么?

    0 回复  |  直到 4 年前
        1
  •  2
  •   ukBaz    4 年前

    您使用的是什么D-Bus绑定?

    我发现 pydbus 在Python中使用绑定要容易得多。例如:

    (venv) pi@raspberrypi:~/stack_overflow $ python3
    Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
    [GCC 8.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pydbus
    >>> udisk2 = pydbus.SystemBus().get('.UDisks2')
    >>> udisk2.on <-tab complete pressed->
    udisk2.onInterfacesAdded    udisk2.onInterfacesRemoved  udisk2.onPropertiesChanged  
    >>> udisk2.onInterfacesAdded = print
    >>> 
    
        2
  •  0
  •   Jürgen Hötzel    4 年前

    您很可能忘记设置事件循环(将示例转换为普通函数):

    import dbus
    from dbus.mainloop.glib import DBusGMainLoop
    from gi.repository import GLib
    
    
    def registerSignals():
        for signal, callback, bus, interface in (('InterfacesAdded', onUSBInterfaceAdded, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager'),
                                                 ('InterfacesRemoved', onUSBInterfaceRemoved, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager'),
                                                 ('PropertiesChanged', onUSBMountPropertiesChanged, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager')):
            print(dbus.SystemBus().add_signal_receiver(callback,
                                                       bus_name=bus,
                                                       signal_name=signal,
                                                       dbus_interface=interface))
    
    
    def onUSBInterfaceAdded(path, interfaces):
        print(path)
        print(interfaces)
    
    
    def onUSBInterfaceRemoved(path, interfaces):
        print(path)
        print(interfaces)
    
    
    def onUSBMountPropertiesChanged(interface, properties, invalidated, path):
        print(path)
        print(interfaces)
    
    
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()
    registerSignals()
    GLib.MainLoop().run()