[Python-Dev] Stackless Python - Pros and Cons

Gordon McMillan gmcm@hypernet.com
Sun, 6 Aug 2000 19:34:44 -0400


Jack Jansen wrote:

> Could the defenders of Stackless Python please explain _why_ this
> is such a great idea? Just and Christian seem to swear by it, but
> I'd like to hear of some simple examples of programming tasks
> that will be programmable in 50% less code with it (or 50% more
> understandable code, for that matter).

Here's the complete code for the download of a file (the data 
connection of an FTP server):

    def _doDnStream(self, binary=0):
        mode = 'r'
        if binary:
            mode = mode + 'b'
        f = open(self.cmdconn.filename, mode)
        if self.cmdconn.filepos:
            #XXX check length of file
            f.seek(self.cmdconn.filepos, 0)
        while 1:
            if self.abort:
                break
            data = f.read(8192)
            sz = len(data)
            if sz:
                if not binary:
                    data = '\r\n'.join(data.split('\n'))
                self.write(data)
            if sz < 8192:
                break

[from the base class]
    def write(self, msg):
        while msg:
            sent = self.dispatcher.write(self.sock, msg)
            if sent == 0:
                raise IOError, "unexpected EOF"
            msg = msg[sent:]

Looks like blocking sockets, right? Wrong. That's a fully 
multiplexed socket. About a dozen lines of code (hidden in 
that dispatcher object) mean that I can write async without 
using a state machine. 

stackless-forever-ly y'rs

- Gordon