[Python-ideas] The async API of the future: Twisted and Deferreds

Antoine Pitrou solipsis at pitrou.net
Sat Oct 13 08:14:45 CEST 2012


On Fri, 12 Oct 2012 15:11:54 -0700
Guido van Rossum <guido at python.org> wrote:
> 
> > 2. Method dispatch callbacks:
> >
> >     Similar to the above, the reactor or somebody has a handle on your
> > object, and calls methods that you've defined when events happen
> >     e.g. IProtocol's dataReceived method
> 
> While I'm sure it's expedient and captures certain common patterns
> well, I like this the least of all -- calling fixed methods on an
> object sounds like a step back; it smells of the old Java way (before
> it had some equivalent of anonymous functions), and of asyncore, which
> (nearly) everybody agrees is kind of bad due to its insistence that
> you subclass its classes. (Notice how subclassing as the prevalent
> approach to structuring your code has gotten into a lot of discredit
> since 1996.)

But how would you write a dataReceived equivalent then? Would you have
a "task" looping on a read() call, e.g.

@task
def my_protocol_main_loop(conn):
    while <some_condition>:
        try:
            data = yield conn.read(1024)
        except ConnectionError:
            conn.close()
            break

I'm not sure I understand the problem with subclassing. It works fine
in Twisted. Even in Python 3 we don't shy away from subclassing, for
example the IO stack is based on subclassing RawIOBase, BufferedIOBase,
etc.

Regards

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net





More information about the Python-ideas mailing list