[Twisted-Python] (Un)Twisted Clients and Servers (newbie)

Bob Ippolito bob at redivi.com
Wed May 26 09:55:22 EDT 2004


On May 26, 2004, at 8:26 AM, Bill la Forge wrote:

> Finally wrote my first Twisted-based client and
> server:
> http://compstrm.sourceforge.net/twcs/csprotocol.html
>
> Both the client and server are implemented as light-
> weight processes, where pipes are used for reading
> and writing to remote processes. This allows the
> logic flow to be more straight-line.
>
> I'm also "translucently" switching threads in the
> client, using in-line method calls, so that reads
> and writes to the client do not block the main thread.
>
> (I'm trying to show how much clearer asynchronous
> programming can be using Computational Streams.)

You really need to take a look at Stackless Python.  It's a much better 
fit for the programming paradigm you want, and tasklet communication is 
WAY faster than thread communication in Python.  I also think that the 
Communicating Sequential Processes model is much nicer than your 
Computational Streams model.

As far as switching between "async" and 'blocking", why don't you just 
yield BlockingMode and yield AsyncMode?  That would kill two birds with 
one stone.  What's the difference between yielding True, False, and 
None (that is rhetorical, I read the implementation)?  Personally I 
think you should use useful names, even if they are just aliases for 
built-in python datatypes.  For example, the following lines are much 
more self-documenting:

yield Cooperate
yield Stop
yield BlockingMode
yield AsyncMode

When I did a framework very similar to but much more simple than yours, 
I also made it possible to integrate with Deferred (to block on it and 
resume on success/err, and also to return a deferred instead of a 
generator using a function wrapper).

Translucent thread migration isn't the greatest idea, sometimes you 
need to tie data structures to the a threadstate (for C level code.. 
something like database cursors, or whatever).  It's usually better to 
keep the same code on the same thread whenever possible.  But of 
course, it's better just to not use them at all unless you are calling 
some blocking C code, because it doesn't do anything for you with 
Python bytecode.

-bob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2357 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20040526/8720e89e/attachment.bin 


More information about the Twisted-Python mailing list