应用程序的一些背景:
- 客户发出网络请求
- 服务器必须处理网络请求(每个请求需要 20 秒)
- 向客户发送回复。
我的方法是首先使用 webpy 和 mod_wsgi 并行化请求服务部分。如果我以 20 个左右的线程开始代码,但由于每个线程需要 20 秒才能完成,因此请求级多线程的用处较小。所以我必须减少 20 秒生成小绿叶的时间。像这样的事情
文件 wordprocess_gevent.py
import gevent
urls = ('/'. A)
class A:
output = {}
def POST(self):
#words is a list of words
method A(words):
def A(self, words):
threads = []
for word in words:
i_thread = gevent.spawn(B, word)
threads.append(i_thread)
gevent.joinall(threads, timeout=2)
def B(self, word):
#Process word takes 20 seconds
result = process(word)
a[word] = result
application = web.application(urls, globals()).wsgifunc()
我使用 mod_wsgi-express 开始此代码,如下所示:
mod_wsgi-express start-server wordprocess_gevent.py --processes 5 --server-root wsgi_logs/ --with-wdb &
当同时 POST 请求到达时,我收到此错误
LoopExit: This operation would block forever
在行
gevent.joinall(threads, timeout=2)
但是如果我发布一个 POST 请求 - 我会得到所需的结果。有人可以帮我吗?
最佳答案
所以我通过完全删除 mod_wsgi 解决了这个问题。即使只有一个进程,当多个请求到来时我也会得到相同的结果。
添加了以下内容,现在它就像一个魅力:)
if __name__ == "__main__":
application = web.application(urls, globals()).wsgifunc()
appserver = WSGIServer(('', 8000), application)
appserver.serve_forever()
关于python - Gevent joinall 阻塞错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25133413/