Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn worker timeout

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?

like image 536
Anton Avatar asked Mar 17 '13 16:03

Anton


1 Answers

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"
like image 100
Incognos Avatar answered Oct 18 '22 23:10

Incognos