python - 使用带有 grequests 的 django 服务器时出现神秘错误

标签 python django gevent grequests

目前,我在 Ubuntu 14.04 上运行一个 vagrant 服务器,并使用简单的 python manage.py runserver 0.0.0.0:8000 测试我的所有 django 模块。

因为我通过http://localhost:8000使用chrome连接到django网络服务器并且服务器在虚拟机上运行,​​我通过使用 Vagrantfile 中的以下设置进行端口转发

config.vm.network "forwarded_port", guest: 8000, host: 8000

一切正常运行(所有模块/ View /测试均按预期运行),但是,自从我开始使用 grequests我收到这个奇怪的错误

Exception happened during processing of request from ('10.0.2.2', 63520)
Traceback (most recent call last):
  File "/home/vagrant/anaconda3/lib/python3.6/socketserver.py", line 639, in process_request_thread
    self.finish_request(request, client_address)
  File "/home/vagrant/anaconda3/lib/python3.6/socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/home/vagrant/anaconda3/lib/python3.6/socketserver.py", line 696, in __init__
    self.handle()
  File "/home/vagrant/anaconda3/lib/python3.6/site-packages/django/core/servers/basehttp.py", line 159, in handle
    self.raw_requestline = self.rfile.readline(65537)
  File "/home/vagrant/anaconda3/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "/home/vagrant/anaconda3/lib/python3.6/site-packages/gevent/_socket3.py", line 385, in recv_into
    self._wait(self._read_event)
  File "/home/vagrant/anaconda3/lib/python3.6/site-packages/gevent/_socket3.py", line 157, in _wait
    self.hub.wait(watcher)
  File "/home/vagrant/anaconda3/lib/python3.6/site-packages/gevent/hub.py", line 651, in wait
    result = waiter.get()
  File "/home/vagrant/anaconda3/lib/python3.6/site-packages/gevent/hub.py", line 899, in get
    return self.hub.switch()
  File "/home/vagrant/anaconda3/lib/python3.6/site-packages/gevent/hub.py", line 630, in switch
    return RawGreenlet.switch(self)
gevent.hub.LoopExit: ('This operation would block forever', <Hub at 0x7f3b777e8af8 epoll pending=0 ref=0 fileno=34>)

请注意,我没有使用 grequests并且简单地导入即使没有被调用或没有任何东西,它似乎也会导致此错误

大家有什么想法吗?

最佳答案

这是底层依赖项之一 - gevent 的问题,它会覆盖 python 内置函数的默认行为,例如 time 等。

你必须进行猴子补丁。 像这样的东西:

from gevent import monkey
monkey.patch_all()

这是相关的gevent documentation .

我最近遇到了这个问题 - 因此停止使用 grequests 并实现了我自己的异步请求逻辑

关于python - 使用带有 grequests 的 django 服务器时出现神秘错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43444959/

相关文章:

python - curve_fit 和 scipy.odr 的比较 - 绝对西格玛

Python getter 、 setter

python - 在 Python 中,为什么 [2] 小于 (1,)?

python - 为什么我的 except 子句从未捕获到 gevent 超时异常?

python - grequests 以什么方式异步?

python - 源不可用时调试python字节码

django - 找不到页面 (404) - 没有帖子与给定的查询匹配

Django AND .htaccess 重写/重定向,这可能吗?

python - 值错误 : invalid literal for int() with base 10: '' Unable to spot error

python - 使用 python 和 gevent 的 http 服务器(不使用 apache)