Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gevent multicore usage

Tags:

I'm just started with python gevent and I was wondering about the cpu / mulitcore usage of the library.

Trying some examples doing many requests via the monkeypatched urllib I noticed, that they were running just on one core using 99% load.

How can I use all cores with gevent using python? Is there best practice? Or are there any side-effects using multiple processes and gevent?

BR dan

like image 330
thesonix Avatar asked Mar 25 '13 14:03

thesonix


People also ask

Is gevent asynchronous?

Gevent functions aren't running asynchronously.

What is the use of gevent?

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.

How do I stop gevent?

you can use gevent. killall() !

How do you use gevent Gunicorn?

By default, Gunicorn uses a synchronous worker class to serve requests, but it can be easily configured to use gevent by simply adding -k gevent to the run command.


1 Answers

Gevent gives you the ability to deal with blocking requests. It does not give you the ability to run on multi-core.

There's only one greenlet (gevent's coroutine) running in a python process at any time. The real benefit of gevent is that it is very powerful when it deals with I/O bottlenecks (which is usually the case for general web apps, web apps serving API endpoints, web-based chat apps or backend and, in general, networked apps). When we do some CPU-heavy computations, there will be no performance-gain from using gevent. When an app is I/O bound, gevent is pure magic.

There is one simple rule: Greenlets get switched away whenever an I/O-operation would block or when you do the switch explicitly (e.g. with gevent.sleep() )

The built-in python threads actually behave in the same (pseudo) "concurrent" way as gevent's greenlets.

The key difference is this - greenlets use cooperative multitasking, where threads use preemptive multitasking. What this means is that a greenlet will never stop executing and "yield" to another greenlet unless it uses certain "yielding" functions (like gevent.socket.socket.recv or gevent.sleep).

Threads, on the other hand, will yield to other threads (sometimes unpredictably) based on when the operating system decides to swap them out.

And finally, to utilize multi-core in Python - if that's what you want - we have to depend on the multiprocessing module (which is a built-in module in Python). This "gets around GIL". Other alternatives include using Jython or executing tasks in parallel (on different CPUs) using a task queue, e.g. Zeromq.

I wrote a very long explanation here - http://learn-gevent-socketio.readthedocs.org/en/latest/. If you care to dive into the details. :-D

like image 54
Calvin Cheng Avatar answered Oct 05 '22 09:10

Calvin Cheng