Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a greenthread equal to a "real" thread

I've taken sample code from Unterstanding eventlet.wsgi.server.

from eventlet import wsgi
import eventlet
from eventlet.green import time
import threading

def hello_world(env, start_response):
    print "got request", eventlet.greenthread.getcurrent(), threading.currentThread()
    time.sleep(10)
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello, World!\n']

wsgi.server(eventlet.listen(('', 8090)), hello_world)

When I access the web server via different client ip addresses, I can see they are processed in parallel. And with the print in hello_world, I can also that they are processed in two different greenthreads but in same OS thread.

I'm new to Python. I'm curious that if each greenthread ties to an underlying OS thread?

like image 474
TieDad Avatar asked Mar 08 '13 05:03

TieDad


People also ask

Are Goroutines green threads?

goroutines are not OS threads, but they are not exactly Green Threads as well. Go's mechanism for hosting goroutines is an implementation of what's called an M:N scheduler , which means it maps M green threads to N OS threads. Goroutines are then scheduled onto the green threads.

What is GreenThread Eventlet?

The GreenThread class is a type of Greenlet which has the additional property of being able to retrieve the return value of the main function. Do not construct GreenThread objects directly; call spawn() to get one. cancel(*throw_args) Kills the greenthread using kill() , but only if it hasn't already started running.

Does rust use green threads?

Rust runs system threads natively; it supports green threads through third-party libraries like Tokio.

Does Python use green threads?

In python, we implement greenlets via the gevent package and we implement pthreads via python's built-in threading module. Both green threads (greenlets) and POSIX threads (pthreads) are mechanisms to support multithreaded execution of programs.


1 Answers

Each green thread is tied to exactly one Python thread which is tied to exactly one OS thread. In theory, Eventlet could distribute green threads across several Python threads, and consequently, OS threads, but that is a lot of work for very little benefit since Python code does not execute in parallel on CPython [1].

Rule of thumb: if you want to use multiple cores, choose other language with Python your best bet is to run several processes. Quick way is multiprocessing[2] (in stdlib since 2.6), robust way is os.fork[3][4] manually.

Just a little clarification on terminology: For most popular operating systems, the only way to execute code in parallel is to have multiple OS threads. Strictly speaking, your requests are processed not in parallel but concurrently; precisely because there is only one OS thread. At any given moment in time there is only one green thread executing some code. Incidentally, the same restriction applies to regular Python threads, that's why Eventlet (or other green thread libraries) mostly just work as drop-in replacement and (mostly) do not cause any new unusual bugs.

You may find this answer useful: https://stackoverflow.com/posts/14227272/revisions

[1] http://wiki.python.org/moin/GlobalInterpreterLock
[2] http://docs.python.org/2/library/multiprocessing.html
[3] http://docs.python.org/2/library/os.html#os.fork
[4] https://github.com/jonashaag/bjoern/blob/master/tests/fork.py

like image 160
temoto Avatar answered Sep 24 '22 20:09

temoto