Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn not reloading a Django application

I'm getting inconsistent code-reloading behavior, with a Django 1.3 application and gunicorn 0.12.1, running inside a virtualenv.

Gunicorn does not reload my application properly, even with a restart of the specific gunicorn process PID. When I run a basic runserver (through Django, via the manage.py command) this is not an issue.

When I remove and recreate my virtualenv, gunicorn runs as expected with the new code.

Is there a Python cache or something? I also tried to remove all *.pyc files.

like image 315
k3k Avatar asked Apr 22 '11 09:04

k3k


People also ask

How do I restart my Django Gunicorn?

If you update your Django application, you can restart the Gunicorn process to pick up the changes by typing: sudo systemctl restart gunicorn.

How does Gunicorn work with Django?

Gunicorn takes care of everything which happens in-between the web server and your web application. This way, when coding up your a Django application you don't need to find your own solutions for: communicating with multiple web servers. reacting to lots of web requests at once and distributing the load.

Does Gunicorn need nginx?

It is recommended in Gunicorn docs to run it behind a proxy server. Technically, you don't really need Nginx. BUT it's the Internet: your server will receive plenty of malformed HTTP requests which are made by bots and vulnerability scanner scripts.

Where is Gunicorn PID file?

your pid file location is /run/gunicorn/gunicorn.


2 Answers

Try this:

$ kill -HUP masterpid

Also, have a look at some of the notes at the bottom of the following post.

like image 100
Honza Pokorny Avatar answered Sep 18 '22 19:09

Honza Pokorny


I ran into variations of this problem as well -- as advised in the article linked to by Mr. Pokomy, killing the gunicorn master process with a HUP signal seems to do the trick.

One can set up auto-reloading on file save easily, if you use the python watchdog module; the setup is actually pretty self-explanatory, so here's a snippet from my development supervisord.conf file:

[program:ost2]
autostart=true
command=/usr/local/share/python/gunicorn --debug\
-c /Users/fish/Dropbox/ost2/ost2/utils/gunicorn/ost2-debug.py wsgi_debug
directory=/Users/fish/Dropbox/ost2/ost2
priority=500
; (etc)

[program:ost2-reloader]
autostart=true
autorestart=false
directory=/tmp
command=/usr/local/share/python/watchmedo shell-command\ 
--patterns="*.py;*.txt;*.html;*.css;*.less;*.js;*.coffee"\
-R --command='kill -HUP $(cat /usr/local/gunicorn/gunicorn.pid)'\
/Users/fish/Dropbox/ost2/ost2/
priority=996
; (etc)

(N.B. I put the slashes in that sample before newlines that aren't actually in the conf file -- I inserted those newlines for legibility; I am not sure if that works IRL)

The first program is the gunicorn process, which I run in a single thread during development in order to use the Werkzeug debugger. The second part is the interesting bit: that command says, "kill the process specified by the gunicorn PID file whenever there's a change in a file in this directory tree if the file's suffix matches one from this list".

Works like a charm for many including me. If you don't know it, watchdog is very useful and is worth a look, in its own right.

like image 40
fish2000 Avatar answered Sep 20 '22 19:09

fish2000