[Python-Dev] Withdrawn PEP 288 and thoughts on PEP 342

Michael Sparks Michaels at rd.bbc.co.uk
Fri Jun 17 11:12:25 CEST 2005


At 08:24 PM 6/16/2005 -0400, Raymond Hettinger wrote:
> As a further benefit, using
>attributes was a natural approach because that same technique has long
>been used with classes (so no new syntax was needed and the learning
>curve was zero).

On Friday 17 Jun 2005 02:53, Phillip J. Eby wrote:
> Ugh.  Having actually emulated co-routines using generators, I have to tell
> you that I don't find generator attributes natural for this at all;
> returning a value or error (via PEP 343's throw()) from a yield expression
> as in PEP 342 is just what I've been wanting.

We've been essentially emulating co-routines using generators embedded
into a class to give us the equivalent of generator attributes. We've found
this very natural for system composition. (Essentially it's a CSP type system, 
though with an aim of ease of use)

I've written up my talk from ACCU/Python UK this year, and it's available
here: http://www.bbc.co.uk/rd/pubs/whp/whp113.shtml

I'll also be talking about it at Europython later this month.

At 08:03 PM 6/16/2005 -0700, Guido van Rossum wrote:
>Someone should really come up with some realistic coroutine examples
>written using PEP 342 (with or without "continue EXPR").

On Friday 17 Jun 2005 05:07:22, Phillip J. Eby wrote:
> How's this?
> 
>    def echo(sock):
>        while True:
>            try:
>                data = yield nonblocking_read(sock)
>                yield nonblocking_write(sock, data)
... snip ...

For comparison, our version of this would be:

from Axon.Component import component
from Kamaelia.SimpleServerComponent import SimpleServer
class Echo(component):
   def mainBody(self):
      while True:
         if self.dataReady("inbox"):
            self.send(data,"outbox")
         yield1

SimpleServer(protocol=EchoProtocol, port=1501).run()


For more interesting pipelines we have:

pipeline(TCPClient("127.0.0.1",1500),
         VorbisDecode(),
         AOAudioPlaybackAdaptor()
        ).run()

Which works in the same way as a Unix pipeline. I haven't written the 
"pipegraph" or similar component yet that could allow this:

graph(A=SingleServer("0.0.0.0", 1500),
           B=Echo(),
           layout = { "A:outbox": "B:inbox", "B:outbox" : "A:inbox" } )

(Still undecided on API for that really, currently the above is a lot more 
verbose -)

By contrast I really can't see how passing attributes in via .next() helps 
this approach in any way (Not that that's a problem for us :).

I CAN see though it helps if you're taking the approach for generator
composition if you're using twisted.flow (though I'll defer a good example
for that to someone else since although I've been asked for a comparison in 
the past, I don't think I'm sufficiently twisted to do so!). 


Michael.
-- 
Michael Sparks, Senior R&D Engineer, Digital Media Group
Michael.Sparks at rd.bbc.co.uk, http://kamaelia.sourceforge.net/
British Broadcasting Corporation, Research and Development
Kingswood Warren, Surrey KT20 6NP

This e-mail may contain personal views which are not the views of the BBC.


More information about the Python-Dev mailing list