独角兽工人超时
我在Nginx后面的Gunicorn中运行了一个Django应用程序.一切正常,针对一件奇怪的事情进行说明:我有一个下载"视图和一个RESTful json API.调用下载视图时,我使用urllib2访问json API以获取信息.确切地说,当我尝试向json api执行此http get请求时,该请求超时并显示错误HTTP错误504:网关超时.
I have a Django application running in Gunicorn behind Nginx. Everything works fine, exect for one strange thing: I have a "download" view and a RESTful json API. When call the download view I use urllib2 to access the json API to get information. And excactly when I try to do this http get request to the json api, the request times out with an error HTTP Error 504: Gateway Time-out.
当我使用./manage.py runserver运行代码时,一切正常.到json api的http get请求也只需几毫秒,因此没有发生超时的危险.
When I run the code with ./manage.py runserver everything works fine. The http get request to the json api also only takes a few miliseconds, so no danger of running into a timeout.
以下是伪代码中的情况:
Here the Situation in Pseudo code:
myproject/views.py:(可通过以下方式访问: http://myproject.com/download )
myproject/views.py: (accessible as: http://myproject.com/download)
1 def download(request, *args, **kwargs):
2 import urllib2
3 opener = urllib2.build_opener()
4 opener.open('http://myproject.com/api/get_project_stats')
在Gunicorn中运行时,第四行中的opener.open()
调用会超时,当与./manage.py runserver
一起运行时,一切正常(并且api调用只需要几毫秒.
The opener.open()
call in line four runs into a timeout when running in Gunicorn, when running with ./manage.py runserver
everytihng works fine (and the api call only takes a few miliseconds.
有人遇到过同样的问题吗?更重要的是:您是如何解决的?
Has anyone had the same problem? And more important: How have you solved it?
I had the same issue using Gunicorn, nGinx, Django and Requests
我每次都做:
response = requests.get('http://my.url.com/here')
工人会超时
我通过从同步(sync)工作者转换为异步(eventlet)工作者解决了这个问题.
I solved the problem by switching from Syncronous (sync) workers to Asynchronous (eventlet) workers.
如果要启动命令行,请添加:
if you are launching command line add:
-k 'eventlet'
如果您使用的是配置文件,请添加:
if you are using a config file add:
worker_class = "eventlet"