12

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.

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: (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')

The opener.open() call in line four runs into a timeout when running in Gunicorn, when running with ./manage.py runservereverytihng 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?

Anton
  • 936
  • 1
  • 8
  • 27

1 Answers1

15

I had the same issue using Gunicorn, nGinx, Django and Requests

every time I did:

response = requests.get('http://my.url.com/here')

the workers would timeout

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"
Sina Khelil
  • 2,001
  • 1
  • 18
  • 27
  • Thank's for the answer! This is it! (But I did not solve this with asynchronous eventlet workers. I solved it with doing the HTTP request in an async task using celery.) – Anton Jun 10 '13 at 13:56
  • 1
    You have to install eventlet as well. I used `pip install eventlet` and then adding `--worker-class eventlet` to the commandline. – Gesias Oct 30 '13 at 15:28
  • `eventlet` is risky, everypart of your code needs to be async and libraries monkeypatched. It could have unintended consequences. – garg10may Sep 18 '18 at 09:09