Since introduction of coroutines required quite a few changes in the implementation of CPython itself, I wonder how gevent
(greenlets
actually, as it turned out) manage to do cooperative multitasking without modifying the interpreter, as for example Stackless Python project did. Was it implemented by manually saving and restoring current execution frame in Python using inspect
? (probably not) Or by having a smart C-extension that hacked into an actual stack of a Python interpreter and used hacks for getting coroutines in C? Or the C extension saved full Python internal execution frame while being "outside" of the execution itself? I am curious about the general idea. Taking a short look at official docs does not seem to give an answer right away.
I am not talking about the event loop implementations or epoll
ing but rather the "manual context switching" / "cooperative multitasking" within a single process.
UPD: It is a duplicate of this question, but I did not figure out how to mark it accordingly. However it is still not clear whether they use something like longjmp
or setcontext
?