I really like the Python 2.5 generator extensions. I have cases where I want a generator to pass a reference to itself to another generator, or to store it in a queue etc. Here's how a generator can get its own handle.
Python, 41 lines
The real case is asynchronous socket (and file) I/O.
A 'pollster' coroutine maintains a dictionary of filenos and generator references. After calling poll(), for each fileno that is ready for I/O, the pollster retrieves the generator instance and calls send() with the event flags from poll().
Each I/O coroutine places its socket fileno and a reference to itself in the pollster dictionary when itinitiates an asynchronous I/O request. It then yields, waiting for the pollster to signal I/O ready.