开发者

Python-dbus extra param to add_signal_receiver

I'd like to pass extra param to add_signal_receiver or get somehow path the signal was received from. Now it's defined like that:

bus.add_signal_receiver(handle_signal, 'RemoteDeviceFound', 'org.bluez.Adapter', 'org.bluez', '/org/bluez/hci'+x)

def handle_signal(a开发者_StackOverflow社区ddress, cls, rssi):
    xxxx

I wan't to have many signal receivers at the same time and be able to read 'x' inside handle_signal function.


The Python DBUS documenation has your answer. It provides the following example to pass the sender to the handler function:

def handler(sender=None):
    print "got signal from %r" % sender

iface.connect_to_signal("Hello", handler, sender_keyword='sender')

So, instead of using bus.add_signal_receiver, create an interface for the signal providing object first and then connect to the signal as in the example.


To add to Oben Sonne's answer, add_signal_receiver takes the same arguments:

bus = dbus.SystemBus()
bus.add_signal_receiver(handler,
                        sender_keyword='sender',
                        destination_keyword='destination',
                        member_keyword='member',
                        path_keyword='path',
                        interface_keyword='interface')


If you want the path the signal was received from, do this:

def handler(path=None):
    print("got signal with path %r" % path)

bus.add_signal_receiver(handler, path_keyword="path")

Similarly, you can pass 'sender', 'destination', 'member', and 'interface' as mentioned by others. You cannot forward arbitrary callback information, however.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜