[Twisted-Python] Event Driven Programming 101 question

Hi all, I've got a few questions about Deferreds. I understand the concept better, but I need a little help at the moment. It's apparent to me that a lot's going on behind the scenes between Deferreds and the Reactor, and that twisted.enterprise orchestrates things in a way I don't yet understand. I'm looking at this from the perspective of my little XML-RPC server. A client hits a XML-RPC function, and by the time that function returns its value, it needs all its Deferreds to have generated their results. That leads to this question: When is a Deferred guaranteed to have a result? One answer could be, "When its callback gets called." And that would be true. But what if the function that gets the Deferred needs the result before it can move on? It seems like it'll get the Deferred and then go on its merry way, whether the Deferred has a result or not. And that's confusing to me, because I'm left wondering what happens if my database takes a long time to respond: def xmlrpc_foo(self): result = dbpool.runQuery("select * from gigantic_table") # adbapi return result In this case, what's it gonna be? A Deferred that's evaluated as None because Postgres is still churning, or is Twisted only going to let the code continue once the Deferred has a result? Sorry if it seems I'm not making sense... Event-driven programming is new to me, but I'm a motivated learner. :-) (And if anybody knows of a good general guide to this paradigm, I'm all ears.) Thanks for your help! Steve

On Sat, 2 Aug 2003, Steve Freitas <sflist@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.
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/

On Saturday 02 August 2003 03:37 am, Steve Freitas wrote:
You write a chain of results: for example [ abstract code ] remotecall(mydatabase, getresultset1).addCallback(result1OK) def result1OK(result): remotecall(get result based on result, result).addCallback(allOK) def allOK(result): this is guaranteed to have the result
-- UC -- Open Source Solutions 4U, LLC 2570 Fleetwood Drive Phone: +1 650 872 2425 San Bruno, CA 94066 Cell: +1 650 302 2405 United States Fax: +1 650 872 2417

On Sat, 2 Aug 2003, Steve Freitas <sflist@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.
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/

On Saturday 02 August 2003 03:37 am, Steve Freitas wrote:
You write a chain of results: for example [ abstract code ] remotecall(mydatabase, getresultset1).addCallback(result1OK) def result1OK(result): remotecall(get result based on result, result).addCallback(allOK) def allOK(result): this is guaranteed to have the result
-- UC -- Open Source Solutions 4U, LLC 2570 Fleetwood Drive Phone: +1 650 872 2425 San Bruno, CA 94066 Cell: +1 650 302 2405 United States Fax: +1 650 872 2417
participants (3)
-
Moshe Zadka
-
Steve Freitas
-
Uwe C. Schroeder