Making asynchronous calls

Simon McVittie simon.mcvittie at collabora.co.uk
Thu Feb 7 06:27:17 PST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 07 Feb 2008 at 13:41:50 +0000, Robert Rawlins wrote:
> Looking at the code example you gave below, I notice a few things which are different from my service, namely the references to the async_callbacks=('reply_cb', 'error_cb')) This is something which isn't currently implemented in my service, also, My current function doesn't return False, I have it return the results of the function, opposed to passing it to the return_cb or error_cb methods.

The fact that there are these callbacks, which can be used to return the
results later, is exactly what makes your service implementation
asynchronous - otherwise, there is no way to return from your
implementation to the main loop (so more requests can be processed) without
returning a value for the D-Bus call.

The ordering of events in my example could go like this (exaggerating
the time it takes for "fast" things to happen, for the sake of clarity):

time event
 0.0 main loop receives method call 1
 0.1 MakeHTTPRequest() called
 0.2 g_timeout_add called
 0.3 MakeHTTPRequest() returns None
...
 1.0 main loop receives method call 2
 1.1 MakeHTTPRequest() called
 1.2 g_timeout_add called
 1.3 MakeHTTPRequest() returns None
...
15.2 first timeout expires after 15 seconds
15.3 closure (nested function) runs
15.4 D-Bus reply 1 sent
...
16.2 second timeout expires after 15 seconds
16.3 closure (nested function) runs
16.4 D-Bus reply 2 sent

(The fact that the nested function returns False is just to keep GLib
happy - timeout_add takes a callback function and expects it to return a
boolean, with True meaning "queue this function again to be called again
after the same time". We only want to be called once in my example, so
the callback returns False.)

See:

the dbus-python API docs for @method

dbus-python/tests/test-service.py (methods AsyncWait500ms, AsyncRaise,
AsynchronousMethod)

http://dev.laptop.org/git?p=projects/presence-service;a=blob;f=src/presenceservice.py;h=fa38efd5639c76c595808f8c10bcb04e0ed1c756;hb=81cfc38857a7bcbbe8680e2acf88859ff880caf9#l750
(a rather complex real-world example)

(Oh, and please tell your mail client to wrap long lines :-P )
-----BEGIN PGP SIGNATURE-----

iD8DBQFHqxVFWSc8zVUw7HYRAvRwAJ48jz2pfQdhC4oAcIzGQz6D9+BtcgCfXDn+
cD2xoipMmn7jz9TNKqeNaAc=
=CwQl
-----END PGP SIGNATURE-----


More information about the dbus mailing list