[Twisted-Python] Should I use asynchronous programming in my own modules?

Jürgen Strass jrg718 at gmx.net
Thu Oct 18 11:05:31 EDT 2007

Jean-Paul Calderone schrieb:
> On Thu, 18 Oct 2007 14:41:38 +0200, Jürgen Strass <jrg718 at gmx.net> wrote:
>> To give some example, I'd like to develop my own simplified document 
>> format in XML and a corresponding parser. [...]
> Have you seen Lore?
Not yet. I'll have a look at it, though. I guess to some degree I'm 
reinventing the wheel, but I rather see this a an exercise.
>> The question is this: since parsing and generating large documents 
>> could block the reactor in a twisted app, should I use any of 
>> twisted's asynchronous programming features in this module (for 
>> better integration with twisted) or should I rather develop it in a 
>> traditional way and run it in a thread?
> Incremental parsing is often useful and simpler than the alternative.  If
> you are accepting a document over the network, why buffer it yourself and
> then parse it when you could just be giving each piece directly to the
> parser?  Done this way, it often is the case that even large documents 
> can
> be parsed without blocking for an unreasonable amount of time.
Okay, I agree. To get away somewhat from parsers, the question arises 
for other programming tasks as well, of course.

So I already asked myself how one would translate the example of a 
factorial function in twisted's core documentation to use the reactor's 
scheduling mechanism instead of running it in a thread. I think an 
example of how to divide it into chunks and how to use the reactor would 
be great.

What I tried at first was programming a simple counter this way. It 
would look much similar to the code I presented in reply to Itamar 
Shtull-Trauring's answer. What I'm not sure about is if this is the 
correct way to go for.

Many thanks for all the other points you've answered, it made a lot of 
things much clearer to me.


More information about the Twisted-Python mailing list