Implementing an Asynchronous Method in Python DBus

How to implement async method in Python DBus? Example below:

class LastfmApi(dbus.service.Object):
    def __init__(self):
        bus_name = dbus.service.BusName('fm.lastfm.api', bus=dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, '/')

    @dbus.service.method('fm.last.api.account', out_signature="s")
    def getUsername(self):
        ## How do I get this method done asynchronously ??
        ##  For example, this method should go off and retrieve the "username"
        ##  asynchronously. When this method returns, the "username" isn't available
        ##  immediately but will be made available at a later time.

I am using Twisted glib2 reactor.

Update . I know that this behavior can be implemented. DBus includes a “serial” (unique identifier) ​​for method calls, and the called method has access to that identifier to match “calls”, with “responses”.

+5
source share
1 answer

I have not tried this, but reading the documentation for dbus.service.methodshows the parameter async_callbacks. This parameter seems to be used to provide an asynchronous result. For example:

@dbus.service.method('fm.last.api.account', out_signature="s",
                     async_callbacks=("callback", "errback"))
def getUsername(self, callback, errback):
    reactor.callLater(3, callback, "alice")

API, Deferred, Deferred :

d.addCallbacks(callback, errback)

, , dbus.service.method. , errback, async_callbacks, - , , . , , , .

, , .:) dbus.service.method, , .

(, , , , , , , d- C-, , , . , , , , , .)

+6

All Articles