What is greenlet?

Question:

I am new to gevent.I have read introduction from gevent

They have provided simple examples but I am struggling to understand what greenlet is.From Learning Concurrency.

Greenlets are a very lightweight coroutine written in C that
are cooperatively scheduled. They provide us with a very lightweight thread-
like object that allows us to achieve concurrent execution within our Python
programs without incurring the cost of spinning up multiple threads.

Greenlets are not threads?
How is synhronisation point defined? Could somone explain with examples?

Asked By: MikiBelavista

||

Answers:

Synchronous programming can only do one thing at a time. So while a database query is running, everyone else (say pulling up a webpage via a web framework) has to wait for that to finish.

Gevent makes it Asynchronous by using context switching and events. What does this mean? Think of it like this. You have a queue with stuff waiting for things to happen, meanwhile gevent says, ok you can wait, I am going to move to the next task and start doing stuff while I am waiting for you to finish (like a database read, or waiting for user input) and when you are done, when I go back through my queue and you say you’re ready for the next step, I focus on that for you.

In this way, though still single threaded, the application can be switching between jobs super fast, constantly checking the status to see if it deserves focus or not, meanwhile, other things can get done while it waits for you.

As opposed to multiple threads, that are handled by the OS and heavy, they require their own resources and are expensive to switch between.

Gevent makes converting stuff that would normally use threading to greenlets easy.

Answered By: eatmeimadanish

A greenlet is block of sequential frames on the CPython C stack that can be swapped out to the heap.

This the general fundamental building block needed to implement coroutines.

The native coroutine implementation in Python does not save C frames to the heap. It’s designed to not have important data on the C stack, and then uses send() and yield [from] to unwind/rebuild each frame of the C stack as you suspend/resume the coroutine. The downside to this is that you need to unnecessarily spam chains of yield from‘s throughout your codebase, and suffer the all the overhead of the stack unwinding/rebuilding. Overall, native python generators are pretty weak in terms of performance and syntactic support.

Stackless python avoids using the C stack for Python function calls all together (because if there’s no useful context, what’s the point?). All the needed context is stored in Python frames (which has the python stack, IP, local vars, return frames…etc). PyPy contains Stackless Python, which is part of the reasons it’s performance is so much better.

Greenlets were inspired by Stackless python. As an extension module for CPython, the C stack is still used for Python function calls, but you can swap out blocks of frames (this block "is" the coroutine/greenlet) to the heap on-demand. Now you can mix execution of coroutines however you’d like to achieve your goals. This way you can get something better than generators in CPython without having to completely switch to PyPy.

Answered By: Matviy Kotoniy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.