Making asynchronous calls
Simon McVittie
simon.mcvittie at collabora.co.uk
Thu Feb 7 08:06:03 PST 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Thu, 07 Feb 2008 at 15:06:47 +0000, Robert Rawlins wrote:
> def timeout_func():
> #Call To Web Service Goes Here
> reply_cb(ReturnResultFromWebServiceCall)
> return False
>
> gobject.timeout_add(500, timeout_func)
> Does that look correct? or am I missing something?
What you're missing is that I'm using timeout_add as a silly fake
example of a way to get a callback called later. What you actually want
to do is to make an async request into the web services library, i.e.
arrange for the reply_cb (or error_cb) to be called later *by the web
services library* (or by some of your code that's run by the web
services library).
So it'd look something like:
@method(...)
def DoMyCall(...)
def ok_all_done(x):
reply_cb(x)
webservices.call(foo, bar, onCompleted=lambda x: ok_all_done(x))
where webservices.call is an asynchronous API provided by the library
you're calling into.
(The Python standard library's xmlrpclib module, for instance, can't be
used like this - it only has a synchronous (blocking) API.)
For instance, if your web-services calls were being made using Twisted
<http://twistedmatrix.com/projects/core/documentation/howto/async.html>,
you'd do an async call that returned a Deferred, and then do something
like:
my_deferred.addErrback(error_cb)
my_deferred.addCallback(reply_cb)
Then your error_cb or reply_cb would run at some later point when
Twisted returned to the main loop.
Simon
-----BEGIN PGP SIGNATURE-----
iD8DBQFHqyxrWSc8zVUw7HYRAk/dAJ42HPJYRe/dZSveICFPi8471YHwjQCg8PwD
zbyuIpUG0lae7AxFi/RoEKM=
=mDnF
-----END PGP SIGNATURE-----
More information about the dbus
mailing list