> I think the closest way to do it in Python is to use threads.
> A thread-aware queue which is an iterator at one end and a file-like
> sink at the other, implemented once for all uses. Called Chan in
> Concurrent Haskell. Should be a nice abstraction. Doesn't require
> more magic than is already there.
> Well, so there are three approaches, and the third does fit Python!
This is already implemented: look at the Queue module:
To push an item into the Queue, you use q.put(x); to pull the next
item from it, use x = q.get(). If you want a terminator, all you need
to do is agree on a sentinel value. If you want an iterator, you can
use iter(q, sentinel).
The disadvantage is that it's way slow compared to stackframe-based
solutions, and that's why it's not commonly used as an alternative for
push/pull interfaces except where threads are already on the mind of
--Guido van Rossum (home page: http://www.python.org/~guido/)