[Twisted-Python] Deferring the return within PB

Abe Fettig abe at fettig.net
Sat Aug 9 19:17:30 MDT 2003


On Sat, 2003-08-09 at 20:17, Edmund Dengler wrote:

> I would like to perform a database operation on the server end. How do I
> defer the return statement til the operation is done?
> 
> Roughly speaking:
> 
>     def perspective_transferXXX(self, XXX):
>         ...perform the database operation, defer to doneTransferXXX()...
>         ...do NOT return a value for the perspective call...
> 
>     def doneTransferXXX(self, XXX):
>         ...perform any other cleanup...
>         ...NOW return a value for the perspective call...

Good news - you can add multiple callbacks to a single Deferred, and
they will be called sequentially, with the output of each one passed to
the next.  So, in your example:

	def perspective_transferXXX(self, *args):
		d = deferredFunction()
		d.addCallback(self.doneTransfer)
		return d

	def doneTransfer(self, response):
		return response.split() # or whatever

In this example, the (eventually) returned value of deferredFunction()
gets passed to doneTransfer first, which is free to alter it as
neccessary before actually returning the value to the client.

For a much better explanation, see the "Using Deferreds" HOWTO.

Abe





More information about the Twisted-Python mailing list