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 sequence. 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.