0

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 epolling 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?

Ben Usman
  • 7,969
  • 6
  • 46
  • 66
  • From the gevent home page: "gevent is a coroutine -based Python networking library that uses [greenlet](https://greenlet.readthedocs.io/en/latest/)...", so you should go look into how greenlet works. Apparently it's a C extension and a spinoff of Stackless, but I don't know more than that. – user2357112 Sep 06 '18 at 17:22
  • @user2357112 but, afaik, it can be installed as a normal Python module, so even though greenlet is based on stackless, it does not require modifications to the interpreter and therefore can be installed on top of a normal CPython interpreter, if I am not mistaken; then the question is "how on earth do greenlets work?"; I updated the question accordingly – Ben Usman Sep 06 '18 at 21:40

0 Answers0