from flask import Flask
app = Flask(__name__)
import threading
class SThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
for i in range(1, 1000):
print 0
t = SThread()
t.start()
for i in range(1, 1000):
print 1
t.join()
@app.route('/')
def hello_world():
return 'Hello, World!'
When you start your server like this, gunicorn run:app -b 0.0.0.0:8000
, you will see all 0s and 1s will be in random order, main thread and child thread are running parallel.
当您像这样启动服务器时,gunicorn运行:app -b 0.0.0.0:8000,您将看到所有0和1将以随机顺序运行,主线程和子线程并行运行。
But when you run same piece of code with gunicorn --worker-class eventlet run:app -b 0.0.0.0:8000
, you will see first there will be all 0s and then there will be all 1s. That means main thread and child thread are not running parallel.
但是当你用gunicorn运行相同的代码--worker-class eventlet run:app -b 0.0.0.0:8000时,你会看到首先会有全0,然后会有所有的1。这意味着主线程和子线程没有并行运行。
Is this expected behaviour?
And how can I use eventlet and make use of threading behaviour?
这是预期的行为吗?我如何使用eventlet并利用线程行为?
Edited ::
编辑::
Based on suggestion, I am trying to do something like this to achieve threads like random behaviour and to join these multiple execution streams. But it is running in sequential manner only.
基于建议,我试图做这样的事情来实现随机行为之类的线程并加入这些多个执行流。但它只是以顺序方式运行。
from flask import Flask
app = Flask(__name__)
import eventlet
def background():
for i in range(1, 10000):
print 0
return 42
def callback(gt, *args, **kwargs):
result = gt.wait()
print("[cb] %s" % result)
greenth = eventlet.spawn(background)
for i in range(1, 10000):
print 1
greenth.link(callback)
@app.route('/')
def hello_world():
return 'Hello, World!'
1 个解决方案
#1
0
This "tight loop" doesn't give chance to run other green threads.
这种“紧密循环”没有机会运行其他绿色线程。
for i in range(1, 1000):
print 0
Eventlet / gevent / asyncio / other similar technologies provide cooperative multithreading. So you must write code that cooperates. You may find this answer useful https://*.com/a/14227272/73957
Eventlet / gevent / asyncio /其他类似技术提供协作多线程。所以你必须编写合作的代码。你可能会发现这个答案很有用https://*.com/a/14227272/73957
In more "real code", you'd perform some network IO or wait on synchronisation which would run other green threads implicitly. Otherwise you need to yield control to other green threads explicitly: eventlet.sleep()
在更“真实的代码”中,您将执行一些网络IO或等待同步,这将隐式运行其他绿色线程。否则,您需要明确地控制其他绿色线程:eventlet.sleep()
Unwanted code review: it would help if you decided to use one of eventlet or threading.
不需要的代码审查:如果您决定使用eventlet或线程之一,它会有所帮助。
#1
0
This "tight loop" doesn't give chance to run other green threads.
这种“紧密循环”没有机会运行其他绿色线程。
for i in range(1, 1000):
print 0
Eventlet / gevent / asyncio / other similar technologies provide cooperative multithreading. So you must write code that cooperates. You may find this answer useful https://*.com/a/14227272/73957
Eventlet / gevent / asyncio /其他类似技术提供协作多线程。所以你必须编写合作的代码。你可能会发现这个答案很有用https://*.com/a/14227272/73957
In more "real code", you'd perform some network IO or wait on synchronisation which would run other green threads implicitly. Otherwise you need to yield control to other green threads explicitly: eventlet.sleep()
在更“真实的代码”中,您将执行一些网络IO或等待同步,这将隐式运行其他绿色线程。否则,您需要明确地控制其他绿色线程:eventlet.sleep()
Unwanted code review: it would help if you decided to use one of eventlet or threading.
不需要的代码审查:如果您决定使用eventlet或线程之一,它会有所帮助。