What is coroutine
As the name implies, coroutine refers to co-operative routine. It allows you to suspending and resuming execution at different locations. So, it’s essentially just context switching. Not surprisingly, coroutine is implemented in primitives like setjmp/longjump or ucontext in low level.
In many senarioes, coroutine is a more light-weight alternative of threads. For programming languages with GIL (like Python), coroutine would used to handle concurrency.
Producer and consumer problem
Let’s take a look at classic “producer-consumer” problem. At each time, one coroutine produce products and add them into queue, the other coroutine take products from queue and use it (hmm, sounds like video buffering, right?).
The code below assumes you already have some knowledge of generator.
1 | import time |
Simple enough, send()
is a built-in function of generator. The producer send data to consumer, consumer receives data from yield
.
Coroutine usage
Yes, the famous concurrency library gevent is based on coroutine.
Reference and Recommended Reading:
PEP 342: Coroutines via Enhanced Generators
General concepts: concurrency, parallelism, threads and processes