What is a coroutine? How are they related to concurrency?
On a different note,
in python gevent
library is a coroutine
based networking library which gives you threadlike features like async network requests, without the overhead of creating and destroying threads. The coroutine
library used is greenlet
.
Coroutines and concurrency are largely orthogonal. Coroutines are a general control structure whereby flow control is cooperatively passed between two different routines without returning.
The 'yield' statement in Python is a good example. It creates a coroutine. When the 'yield ' is encountered the current state of the function is saved and control is returned to the calling function. The calling function can then transfer execution back to the yielding function and its state will be restored to the point where the 'yield' was encountered and execution will continue.
I will expand on @user21714 's answer. Coroutines are independent paths of execution that can not run simultaneously. They depend upon a controller - for example a python
controller library - to handle switching between these paths. But for this to work the coroutines themselves need to invoke yield
or similar structures that allow their execution to be paused.
Threads instead are running on independent compute resources and in parallel with each other. Since they are on different resources there is no need for invoking yield to allow the other paths of execution to proceed.
You can see this effect by starting a multihreaded program - e.g. a jvm
application - in which all eight of your core i7
hyperthread cores are utilized: you might see 797% utilization in Activity Monitor
or Top
. Instead when running a typical python
program - even one with coroutines
or python threading
- the utilization will max out at 100%. I.e. one machine hyperthread.
Coroutine is similar to subroutine/threads. The difference is once a caller invoked a subroutine/threads, it will never return back to the caller function. But a coroutine can return back to the caller after executing a few piece of code allowing the caller to execute some of its own code and get back to the coroutine point where it stopped execution and continue from there. ie. A coroutine has more than one entry and exit points
A coroutine is a special kind of subprogram. Rather than the master- slave relationship between a caller and a called subprogram that exists with conventional subprograms, caller and called coroutines are more equitable.
A coroutine is a subprogram that has multiple entries and controls them itself – supported directly in Lua
Also called symmetric control: caller and called coroutines are on a more equal basis
A coroutine call is named a resume
The first resume of a coroutine is to its beginning, but subsequent calls enter at the point just after the last executed statement in the coroutine
Coroutines repeatedly resume each other, possibly forever
Coroutines provide quasi-concurrent execution of program units (the coroutines); their execution is interleaved, but not overlapped
I find most of the answers too technical even though it is a technical question. I had a hard time trying to understand the coroutine process. I kind of get it but then I don't get it at the same time.
I found this answer here very helpful:
https://dev.to/thibmaek/explain-coroutines-like-im-five-2d9
To quote from Idan Arye:
To build on your story, I'd put it something like this:
You start watching the cartoon, but it's the intro. Instead of watching the intro you switch to the game and enter the online lobby - but it needs 3 players and only you and your sister are in it. Instead of waiting for another player to join you switch to your homework, and answer the first question. The second question has a link to a YouTube video you need to watch. You open it - and it starts loading. Instead of waiting for it to load, you switch back to the cartoon. The intro is over, so you can watch. Now there are commercials - but meanwhile a third player has joined so you switch to the game And so on...
The idea is that you don't just switch the tasks really fast to make it look like you are doing everything at once. You utilize the time you are waiting for something to happen(IO) to do other things that do require your direct attention.
Definitely check the link, there are much more that I cannot quote everything.