Implementation of an async method in Python DBus

How do I implement an async method in Python DBus? An 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's glib2 reactor.

Update: I know this behavior is possible to implement - DBus includes a "serial" (unique identifier) to method calls and the called method has access to this identifier in order to match "calls" with "replies".

-------------Problems Reply------------

I haven't tried this, but reading the documentation for dbus.service.method reveals the async_callbacks parameter. It sounds like one uses this parameter 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")

If, instead, you have an API which returned a Deferred, then you can easily associate the Deferred with these callbacks:

d.addCallbacks(callback, errback)

As far as the correlation between call and response goes, I assume that all the serial number handling is hidden inside dbus.service.method. I suspect that the callback and errback functions that are passed in when you use the async_callbacks feature are either instances of some class which is callable and has the serial number as an attribute, or else are defined as nested functions and close over the serial number. This way, when you call one of them, they can make sure to pass the right value back to the connection to associate the response with the original request.

However, that's just a slightly educated guess based on your mention of serial numbers and my experience with implementing various async systems. :) A read of the implementation of dbus.service.method would probably reveal the actual strategy without too much pain.

(Okay, I actually went and looked at the implementation now, and unfortunately it's rather complicated, and I lost the trail when it got to some code that's defined in the C-level dbus bindings, which is a bit more digging than I'm interested in doing. I still suspect the general idea I described above is correct, but the details of the implementation are more involved than I expected.)

Category:python Views:4 Time:2010-01-26

Related post

  • How do you investigate python's implementation of built-in methods? 2012-04-21

    I'm currently going through a basic compsci course. We use Python's in a lot. I'm curious how it's implemented, what the code that powers in looks like. I can think of how my implementation of such a thing would work, but something I've learned after

  • How is split('\n') method in Python implemented? 2011-04-14

    This is a theoretical question to be able to understand a difference between Java and Python. To read the content of a file into an array in Java, you need to know the number of lines, in order to define the size of the array when you declare it. And

  • How to implement Async method calls in Java? 2012-02-28

    Is there is way to implement Async method call in java Other than thread and Executor ? If it is implementing by Executor will it introduce any delay ? --------------Solutions------------- Using Threads is the way in Java. Executors is just a way to

  • Is there any implementation of this string matching method in python? 2011-03-04

    I am trying to work out which entries in my data store are near-duplicates using approximate string matching. Is there any implementation of the following approach in python, or do i need to try and roll my own? Thanks :) from wikipedia: ... A brute-

  • Python Dbus : How to export Interface property 2010-09-18

    In all python dbus documentations there are info on how to export objects, interfaces, signals, but there is nothing how to export interface property. Any ideas how to do that ? Regards, Levon --------------Solutions------------- It's definitely poss

  • What are Class methods in Python for? 2008-09-01

    I'm teaching myself Python and my most recent lesson was that Python is not Java, and so I've just spent a while turning all my Class methods into functions. I now realise that I don't need to use Class methods for what I would done with static metho

  • How to find out the arity of a method in Python 2009-06-13

    I'd like to find out the arity of a method in Python (the number of parameters that it receives). Right now I'm doing this: def arity(obj, method): return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self class Foo: def bar(self,

  • How can I call an async method of a webservice that returns void? 2009-08-26

    The webservice that I am calling from my application has two methods. XmlNode getCase(string parameter) // synchronous void getCaseAsync(string parameter) //async I can simply call method 1 and store the results in an xmlnode like this, XmlNode node=

  • Why is there no list.clear() method in python? 2009-09-09

    Inspired by this question. Why is there no list.clear() method in python? I've found several questions here that say the correct way to do it is one of the following, but no one has said why there isn't just a method for it. del lst[:] lst[:] = [] Wh

  • Async Method calls calling Sync method call 2009-09-23

    I'm developing a thread safe class and I want to wrap async method call around a sync method call. Maybe I'm not looking for a definition of "asynccall" method, but something close to it. When calling AsyncToStartSearchPage() on the instance, I want

  • Overriding set methods in Python 2009-10-26

    I want to create a custom set that will automatically convert objects into a different form for storage in the set (see Using a Python Dictionary as a key non-nested) for background. If I override add, remove, __contains__, __str__, update, __iter__,

  • Should I provide Sync or Async methods for developers in my IMDB API library? 2009-11-02

    I'm going to make a DLL Library that'll alow developers to search and access IMDB movie pages. How should I handle the freezing in the GUI, should I use async methods, or should I allow a dev to manually create a thread for using our DLL? -----------

  • How do I get the selected text in desktop application using python-dbus? 2009-11-16

    For example, I open a pdf file or a web page in gnome, use mouse double click some text, so a word is selected, how can I get this word in a background running daemon written with python-dbus? Some simple but working piece of script is appreciated gr

  • Third-party titlecase method in Python 2010-05-03

    The standard string title() method in Python is pretty naive and doesn't correctly handle converting even fairly simple words and phrases to title case (hyphenated words, phrases with quotes, phrases with prepositions, etc.). In Googling around, I fo

  • Magic Methods in Python 2010-06-14

    I'm kind of new to Python and I wonder if there is a way to create something like the magic methods in PHP (http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods) My aim is to ease the access of child classes in

  • How to implement a performant filecopy method in C# from a network share? 2010-07-06

    I'm trying to implement a filecopy method that can match the performance a copy done with the windows explorer. For exemple a copy (with the windows explorer) from our nas to my computer, performs above 100mb/sec. My current implementation does the s

  • Silverlight Async Method Chaining (Possible gotchas?) 2010-09-03

    I am working on a 'proof of concept' Silverlight 4 project and am learning the way of THE ASYNC. I have stopped fighting the urge to implement some pseudo-synchronous smoke and mirrors technique. I am going to learn to stop worrying and love THE ASYN

  • Try / Catch and Async methods 2011-01-31

    What is the best practice of try\catch exceptions in .net async methods Socket.BeginSend/Socket.EndSend for example:) --------------Solutions------------- if you want to handle the error, you may use try..catch. try { // some logic implement } catch(

  • Implementing a macro recorder for a python gui? 2011-02-23

    I'm wondering how to go about implementing a macro recorder for a python gui (probably PyQt, but ideally agnostic). Something much like in Excel but instead of getting VB macros, it would create python code. Previously I made something for Tkinter wh

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.255 (s). 11 q(s)