[Twisted-Python] Event Driven Programming 101 question

Moshe Zadka m at moshez.org
Sat Aug 2 05:01:46 MDT 2003


On Sat, 2 Aug 2003, Steve Freitas <sflist at ihonk.com> wrote:

> It's apparent to me that a lot's going on 
> behind the scenes between Deferreds and the Reactor

Nope. There's very little magic going on.
 
> When is a Deferred guaranteed to have a result?
> 
> One answer could be, "When its callback gets called." And that would be true. 

Yep.

> But what if the function that gets the Deferred needs the result before it 
> can move on?

It's out of luck.

> def xmlrpc_foo(self):
> 	result = dbpool.runQuery("select * from gigantic_table") # adbapi
> 	return result

This "just" works. When xmlrpc_foo returns a result, the Twisted XMLRPC
machinery checks if it is a deferred. If it is, it won't send a result
back to the client until the deferred is triggered.

Here is the code in question, just to see how little magic there is:

'''
            request.setHeader("content-type", "text/xml")
            defer.maybeDeferred(function, *args).addErrback(
                self._ebRender
            ).addCallback(
                self._cbRender, request
            )
'''

[_ebRender and _cbRender do the work of actually formatting the response]
-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/




More information about the Twisted-Python mailing list