python - Gevent joinall 阻塞错误

标签 python web.py gevent

应用程序的一些背景:

  1. 客户发出网络请求
  2. 服务器必须处理网络请求(每个请求需要 20 秒)
  3. 向客户发送回复。

我的方法是首先使用 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/

相关文章:

python - jupyter notebook 错误的解释器错误消息

nginx FastCGI-去掉位置前缀?

Python gevent pool.join() 永远等待

python - 带有 python-firebase 的 Firebase 抛出 "Connection reset by peer"(Celery + gevent 参与)

python - 从文件加载 mako 模板

python - 在 Django 表单中自动填写用户

python - 如何在 OpenShift 上使用 mongohq 或 mongolab

python - 在 web.py 中异步发送邮件

python - web.py 上的 SSL 证书链不完整

python - 在 Python 中调用并等待(一段时间)异步事件