We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Asyncore and Generators

November 30, 2001 | Fredrik Lundh

I’ve been playing with asyncore and 2.2’s new generator model. Here’s the idea:

Instead of passing events to callbacks, pass information to a single handle function (a generator) via instance attributes. When the handler needs more data from the network, use yield to pass control back to the asyncore dispatcher.

Does it work? You bet.

from YieldAsyncore import *

class httpClient(YieldDispatcherWithSend):

    def __init__(self, host, port, path):
        self.host = host
        self.path = path
        YieldDispatcherWithSend.__init__(self, host, port)

    def handle(self):

        # send request
        self.send(
            "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (self.path, self.host)
            )
        yield OK

        # look for response header
        while self.data:
            try:
                i = string.index(self.data, "\r\n\r\n")
            except ValueError:
                yield MORE # need more data
            else:
                print self, repr(self.data[:i])
                self.data = self.data[i+4:]
                if not self.data:
                    yield OK
                break

        # process response body
        while self.data:
            print self, len(self.data)
            yield OK

        print self, "done"

It’s not co-routines, but it’s pretty damn close!

(In case it’s not obvious, the trick here is that you can create any number of httpClient instances, and let them all run in parallel.)