绿线程是否等于“真实"线程?线 [英] Is a greenthread equal to a "real" thread

查看:124
本文介绍了绿线程是否等于“真实"线程?线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Understanding eventlet.wsgi.server 中获取了示例代码.

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)

当我通过不同的客户端IP地址访问Web服务器时,可以看到它们是并行处理的.通过hello_world中的打印,我还可以在两个不同的greenthreads中但在同一OS线程中对它们进行处理.

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.

我是Python的新手.我很好奇,如果每个greenthread都与底层OS线程相关联?

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

推荐答案

每个绿色线程仅与一个Python线程绑定,而该Python线程与仅一个OS线程绑定.从理论上讲,Eventlet可以在多个Python线程之间分配绿色线程,因此,可以在OS线程之间分配绿色线程,但是由于 Python代码不能在CPython上并行执行,因此这是很多工作,但却没有什么好处[1]

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

经验法则:如果要使用多个内核,请使用Python 选择其他语言,最好的选择是运行多个进程.快速方法是multiprocessing [2](从2.6开始在stdlib中),稳健的方法是手动os.fork [3] [4].

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.

只需对术语进行一些澄清: 对于大多数流行的操作系统,并行执行代码的唯一方法是拥有多个OS线程. 严格来说,您的请求不是并行处理的,而是并行处理的;正是因为只有一个OS线程.在任何给定的时间,只有一个绿色线程执行某些代码.顺便说一句,相同的限制适用于常规Python线程,这就是为什么Eventlet(或其他绿色线程库)通常只用作即插即用替换,并且(大多数)不会引起任何新的异常错误的原因.

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.

您可能会发现此答案很有用: https://stackoverflow.com/posts/14227272/revisions

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

[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

这篇关于绿线程是否等于“真实"线程?线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆