[Twisted-Python] newbie: connecting to a PB service from a web resource

Donovan Preston dp at twistedmatrix.com
Fri Nov 1 08:38:27 MST 2002


It's fairly simple, there's just one important detail you're missing -- 
in order to get a node factory_* to wait for a deferred, you have to 
return it. (Ah, the joys of async programming.) Since you're also 
dealing with a series of deferreds, it's even more tricky. Here's what 
you do:

In the factory_getStatus method, get the first Deferred by calling 
connect. Add a callback to it and make sure you pass the node to the 
callback. Return the Deferred. (DOMTemplate will know that in order to 
render this node, it will have to wait for the results of the Deferred.)

In the readStatus callback method (whose signature should be modified 
to accept the node you passed to the callback above), get the second 
deferred, add the setResult callback to it, and pass the node along to 
that callback as well. *Return the Deferred*. (DOMTemplate sees that 
the results of the first Deferred are a second Deferred. It knows that 
in order to render this node, it will have to wait for the results of 
the second Deferred.)

Finally, in the setResult callback method (whose signature should be 
modified to accept the node), you have the result data you wish to 
insert into the node. You also have a reference to the node, since you 
passed it along though all the callbacks. Mutate the node (clear it, 
then node.appendChild(self.d.createTextNode("%s", result))) and *Return 
the node*. (DOMTemplate sees that the results of the second deferred 
are a node, and it will be inserted into the DOM (in this case, since 
the returned node is the same as the node that was passed in, nothing 
happens) and when the DOM is rendered to XML your data will be in it.)

Why is this fairly complicated procedure required? Well, in the 
original factory_getStatus, you call self.connect() which immediately 
returns a Deferred. You assume that self.result is ready -- this is not 
the case. Since the data will not be ready until the callback has 
fired, you have to defer doing the actual work until the callback, as 
well.

Donovan

On Friday, November 1, 2002, at 05:59 AM, Paul Stevens wrote:

>
>
> Hi all,
>
> I'm trying to build a small webapp that will allow me to connect to a 
> remote PB
> service. Results from the remote calls are to be used in a simple DOM 
> template.
>
> Is this feasable ?
>
> I've got all the building blocks functioning alright, using the 
> pbquote.py and
> pbquoteclient.py code as examples.
>
> However, when I try to integrate the code from the commandline client 
> into the
> dom resource, I'm lost.
>
> How can I make sure the client waits a while -- until all callbacks are
> handled ?
>
> I can see the refcount growing on the server side each time I connect 
> to the
> clientside webserver. But how to disconnect.
>
> Clues or pointers anyone ? Thanks.
>
>
> -- 
>   ________________________________________________________________
>   Paul Stevens                                  mailto:paul at nfg.nl
>   NET FACILITIES GROUP                     PGP: finger paul at nfg.nl
>   The Netherlands________________________________http://www.nfg.nl
> <webstatusreporter.py>





More information about the Twisted-Python mailing list