[Twisted-Python] Advice sought on application evolution

Justin Warren daedalus at eigenmagic.com
Tue Mar 25 03:39:16 EDT 2008

On Sun, 2008-03-23 at 03:48 +0000, glyph at divmod.com wrote:
> On 22 Mar, 01:54 pm, mithrandi at mithrandi.net wrote:
> >But yes, the danger of accidentally serializing operations that don't
> >need to be serialized, and not even realising it, is one of the 
> >problems
> >with this coding style.
> As time goes on, I'm becoming a bigger fan of inlineCallbacks.
> Too much serialization, even accidental serialization, trades 
> comprehensibility for performance.  I think that's a reasonable place to 
> start: make your system easy to understand, then make it fast once you 
> know what's going on.

How does the saying go? "Premature optimisation is the root of all

As an intermediate novice, I find inlineCallbacks to be a great way to
make code look more like sequential code, but with many of the benefits
of asynchrony. You can then refactor the code to make pieces even more
asynchronous where it's useful, but keep the sequential parts where that
makes sense. This is great from a 'learning twisted' point of view. 

The easiest place to start learning is with code that looks like all the
other code you've written before. The familiar is comforting. You then
learn that some extra statements are required to 'turn on this
asynchronous stuff', which you need to explicitly refer to using
'yield'. This introduces you to the idea that a Deferred-returning
function is special. You may not really know *why* it's special, but
it's a start.

Once your understanding deepens, you can then see that certain bits of
your code need a more complex form of Deferred handling. Even better,
you're now learning what a Deferred does in a concrete way with your own
code, and can more easily understand the effects of .addCallback()
compared to .addErrback().

I wonder if a tutorial along these lines would help in introducing
twisted concepts to a novice?

> It's not a good place to end up, though.  So it's important to use 
> inlineCallbacks for the right thing.

Certainly. Learning what that is can take a while, though.

Justin Warren <daedalus at eigenmagic.com>

More information about the Twisted-Python mailing list