[Twisted-Python] Twisted vs jQuery Deferreds (was Re: RPC design questions)
Tobias Oberstein
tobias.oberstein at tavendo.de
Wed Aug 24 18:22:32 EDT 2011
I follow Glyphs advice and only keep the lambda version with standard Twisted Deferred.
However, I've got a (I believe) more serious "design" problem.
The RPC stuff works in Twisted, but also from JavaScript.
So I have 2 audiences (Twisted and JS developers).
On JS, I use the Deferreds that come standard with jQuery since 1.5.
Now compare the following Twisted code (self.call does the RPC and returns a Twisted Deferred):
# prints 23
d2 = self.call("square", 23).addCallback(lambda res: \
self.call("sqrt", res)).addCallback(self.show)
# prints 23
d3 = self.call("square", 23).addCallback(lambda res: \
self.call("sqrt", res).addCallback(self.show))
(note the subtle difference in bracketing)
with the following JS
// prints 529
sess.call("square", 23).then(function(res) {
return sess.call("sqrt", res);
}).then(console.log);
// prints 23
sess.call("square", 23).then(function(res) {
sess.call("sqrt", res).then(console.log);
});
===
The JS is structurally/syntactically similar to the Py version.
However they behave differently.
There seems to be a fundamental difference between Twisted and jQuery Deferreds.
When calling then() on a jQuery Deferred D, it seems to return D, not any Deferred that might
be returned within the handler within then().
http://api.jquery.com/deferred.then
Whereas the Twisted Deferred addCallback() returns the inner Deferred.
Am I getting something wrong?
I'd be glad on any hints/clarification whats going on here ..
Tobias
==
The complete examples are on:
JS:
https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_client.html
Py Client:
https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_client.py
Py Server:
https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_server.py
Library:
https://github.com/oberstet/Autobahn/tree/master/lib/python
=> python setup.py install
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20110824/e0e78d92/attachment.htm
More information about the Twisted-Python
mailing list