Python co-routines: follow-up to Python generators presentation [pdf]
dabeaz.com
dabeaz.com
This is a follow-up presentation.
I would advice against using raw co-routines for co-operative multitasking though, barring a few special cases; gevent/greenlet will be a better option.
Is this because you are new to Python (or programming in general) or because these concepts and use cases were never explained properly, or you simply like Python and up-vote related stories?
(I'm actually asking myself similar questions about PostgreSQL stories as well.)
Anyway, I encourage you to take a look at http://docs.python.org/whatsnew/index.html (and http://docs.python.org/dev/whatsnew/index.html) from time to time.
Posting articles such as this one is very helpful because it pushes useful esoteric knowledge into our consciousness. HN is particularly suited because the community's up-votes convince us that our time will be well spent looking into such articles.
Greenlet is an optimized implementation of coroutines for Python. Gevent or Eventlet combine greenlet + a scheduler and even monkeypatch your blocking calls into nonblocking ones.
I use eventlet for my load testing tool, Micro Army, to create nonblocking SSH pools. I can SSH to 2 boxes and run config scripts in parallel in 58 seconds, but I can SSH to 100 boxes and run the same tasks in 106 seconds.
The code couldn't be simpler too. Here's the proof.
SSH details for a single host: https://github.com/j2labs/microarmy/blob/master/microarmy/fi...
SSH to every host in a list: https://github.com/j2labs/microarmy/blob/master/microarmy/fi...
Notice that the code is basically just spawning a coroutine and iterating across a list of coroutines. Done. No callback spaghetti, yet all of this is async and nonblocking.
Another example using this framework is Simpy (http://simpy.sourceforge.net/), a process based discrete event simulation platform. You program in a way similar to the traditional multi-process/thread manner, but don't have to worry about mutex and locks at all, very sweet and productive!
The only caveat for this framework is that it's hard to fully utilize the power of multi-core systems, since it's single threaded. But if your application is more I/O bound than CPU bound, this framework could potentially give you huge benefit of multi-threading w/o the complexity.