Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is greenlet?

Tags:

python

gevent

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?

like image 443
MikiBelavista Avatar asked Apr 05 '18 09:04

MikiBelavista


People also ask

What is a Greenlet in Python?

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.

How do greenlets work?

The python code of the newly spawned greenlet uses the heap in a normal way and the interpreter continues using the C-stack as usual. When a greenlet is switched to from a switching greenlet, the relevant part of the C-stack (starting from the base address of the switchng greenlet) is copied to the heap.

What is the difference between Greenlet and gevent?

gevent is an open source project written and maintained by Denis Bilenko and is spread under the MIT license. Greenlet is a lightweight coroutine provided to Python as a C extension module. It is a primary pattern used in gevent that runs inside of the OS process for the main program.

What is gevent used for?

gevent is a coroutine -based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev or libuv event loop. Features include: Fast event loop based on libev or libuv. Lightweight execution units based on greenlets.


1 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.

like image 77
eatmeimadanish Avatar answered Nov 14 '22 22:11

eatmeimadanish