[Twisted-Python] deferred object inside function

Duncan McGreggor duncan.mcgreggor at gmail.com
Fri Jul 7 04:48:11 EDT 2006

Yusnel Rojas García wrote:

> how can I make a function which have a deferred object return a value that
> depends of the deferred object's result.
> example:
> how can get the result from the resolver?
> from twisted.names import client, dns
> def somefunction(somepars):
>       r  = client.Resolver('/etc/resolv.conf')
>       d = r.resolve(dns.Query('www.example.com', dns.MX, dns.IN))
>       d.Callbacks(somefunct1, somefunctErr2)
>       return ?

Have you read the docs on deferreds? There's a bunch of good ones now,
and you can get to them here:

In particular, you want the section on "Low-Level Networking and Event

In your example above, you probably want this, instead:

  d.addCallbacks(yourCallback, yourErrback)

As for your question, see if this sums it up:

* You want some data
* The data depends upon the result

If that's correct, then you can only get your data in the callback.
Think about writing a class to handle this, and instead of having a
function return what you want, you could set a class attribute with the
data you're looking for once the callback is fired. Or any other of
hundreds of possible solutions ;-)


More information about the Twisted-Python mailing list