How do coroutines improve performance
I've been seeing a lot of talks and articles on coroutines in python. They are considered to be "microthreads" and I've heard they improve performance.
How do coroutines improve performance? From what I've seen so far, they are single threaded and execute in seque开发者_开发技巧nce. I agree that from a design perspective they are quite elegant especially in producer/consumer type applications.
I guess I am missing the point. Can someone help explain?
Coroutines don't really improve performance except in a very limited sense: multi-threaded programs have a certain overhead and coroutines provide some of the benefit of threading without incurring those overheads. However, most multi-threaded applications (even in C-Python with its GIL) benefit from the overlap when one thread blocks on a system call and other threads can run: that doesn't generally happen with coroutines.
If you have a few threads then usually the overlapping wins out and coroutines give no performance benefit. If you need thousands of threads then the thread switch overheads will be much greater and in that situation coroutines might give a benefit but reducing the number of threads is likely to give much greater benefit.
The real gain of coroutines is that for producer/consumer applications they make the coding much simpler and therefore faster to code.
This is a good question, it reminded me of David Beazley's A Curious Course on Coroutines and Concurrency. David does an excellent job of not only explaining how coroutines work in Python, but the use cases where they're really in the pocket.
His writing seems to indicate that the performance benefit comes from having less overhead to accomplish the same tasks for which you'd typically use a handler class (see slide 51 of his presentation).
So like @Duncan's answer suggests, situations where overhead matters (like having many many threads), coroutines are a performance win, but coroutines are about a lot more than just performance.
精彩评论