Generators are great if you ask me and so are the power ups that Python 2.5 adds to them. Since the last version of Python you have:
yield as an expression so you can store a returned value
values can be sent to the generator with gen.send(value), so you can somewhat handle the generator from the outside [...]
This is an addition of the post Producer/Consumer in Python I wrote earlier.
It’s just the same example rewritten with py.magic.greenlet.
Greenlets, tasklets, micro-threads, coroutines are basically the same thing: suspending and resuming a function in multiple points is the core feature we need.
In the previous example we did it with plain old generators, now we try [...]
Like Hello World in imperative programming the Producer/Consumer problem is one of kind of program everybody has wrote once in a lifetime.
It’s one of the toy examples in concurrent programming demonstrating the basics of synchronization.
Basically there’s a shared buffer, a producer entity and a consumer one. The producer generates some data and puts it in [...]