[Twisted-Python] raw deferreds vs deferredGenerator vs inlineCallbacks
I wrote a short example comparing the 3. Prepared it mostly for myself, but maybe it will be of some use for somebody: http://blog.mekk.waw.pl/archives/14-Twisted-inlineCallbacks-and-deferredGene... PS Crucial function: raw deferreds - 29 non-empty lines of code, deferredGenerator - 26 lines, inlineCallbacks - 17 lines -- ---------------------------------------------------------------------- | Marcin Kasperski | Systems built by humans are always subject | http://mekk.waw.pl | to human error. (Parnas) | | ----------------------------------------------------------------------
On Wed, 13 Aug 2008 19:47:53 +0200, Marcin Kasperski <marcin.kasperski@softax.com.pl> wrote:
I wrote a short example comparing the 3. Prepared it mostly for myself, but maybe it will be of some use for somebody:
http://blog.mekk.waw.pl/archives/14-Twisted-inlineCallbacks-and-deferredGene...
PS Crucial function: raw deferreds - 29 non-empty lines of code, deferredGenerator - 26 lines, inlineCallbacks - 17 lines
Nice comparison. A more idiomatic rendering of the "raw deferred" example might look like this, though: def lookup(country, search_term): query = "http://www.google.%s/search?q=%s" % (country,search_term) d = getPage(query) def cbFirstPage(content, country): m = re.search('<div id="?res.*?href="(?P<url>http://[^"]+)"', content, re.DOTALL) if not m: return None url = m.group('url') d = getPage(url) def cbSecondPage(content, country, url): m = re.search("<title>(.*?)</title>", content) if m: title = m.group(1) return dict(url = url, title = title) else: return dict(url=url, title="<not-specified>") d.addCallback(cbSecondPage, country, url) return d d.addCallback(cbGotPage, country) def ebGeneric(err, country): print ".%s FAILED: %s" % (country, str(e)) d.addErrback(ebGeneric, country) return d Converting any failure into a print and a None result is a bit strange, but it's the same thing your other two examples did so I preserved that behavior. Jean-Paul
Nice comparison. A more idiomatic rendering of the "raw deferred" example might look like this, though: (...)
Hmm, I'd be glad to add this to the article in a way or another, would you mind it? (btw, I find my "raw" example more readable, but this is likely a matter of taste...)
Converting any failure into a print and a None result is a bit strange,
Just wanted to do something illustrating error handling
On Wed, 13 Aug 2008 20:28:24 +0200, Marcin Kasperski <marcin.kasperski@softax.com.pl> wrote:
Nice comparison. A more idiomatic rendering of the "raw deferred" example might look like this, though: (...)
Hmm, I'd be glad to add this to the article in a way or another, would you mind it?
Sure, that's fine with me. Jean-Paul
On 05:47 pm, marcin.kasperski@softax.com.pl wrote:
I wrote a short example comparing the 3. Prepared it mostly for myself, but maybe it will be of some use for somebody:
Thanks for writing this up! It always makes me happy to see the ecosystem around Twisted developing, people blogging about stuff like this. Makes it feel like a real project. Of course it would make it feel even more like a real project if you could be convinced to contribute to the official documentation :). There's no mention of inlineCallbacks in the official documentation (the stuff in doc/) at all, and it would be helpful to have at least an overview.
participants (3)
-
glyph@divmod.com
-
Jean-Paul Calderone
-
Marcin Kasperski