gunicorn autoreload on source change
--
Track title: CC F Haydns String Quartet No 53 in D
--
Chapters
00:00 Question
00:36 Accepted answer (Score 316)
00:54 Answer 2 (Score 22)
01:22 Answer 3 (Score 15)
01:51 Answer 4 (Score 7)
03:08 Thank you
--
Full question
https://stackoverflow.com/questions/1277...
Accepted answer links:
[--reload]: https://docs.gunicorn.org/en/stable/sett...
Answer 2 links:
[--max-requests]: http://docs.gunicorn.org/en/latest/confi...
Answer 3 links:
[Bryan Helmig]: http://bryanhelmig.com/auto-reload-gunic.../
Answer 4 links:
https://mikeeverhart.net/2013/01/using-g.../
https://www.cyberciti.biz/faq/linux-unix.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #reload #gunicorn
#avk47
ACCEPTED ANSWER
Score 344
While this is old question you need to know that ever since version 19.0 gunicorn has had the --reload option.
So now no third party tools are needed.
ANSWER 2
Score 25
One option would be to use the --max-requests to limit each spawned process to serving only one request by adding --max-requests 1 to the startup options. Every newly spawned process should see your code changes and in a development environment the extra startup time per request should be negligible.
ANSWER 3
Score 17
Bryan Helmig came up with this and I modified it to use run_gunicorn instead of launching gunicorn directly, to make it possible to just cut and paste these 3 commands into a shell in your django project root folder (with your virtualenv activated):
pip install watchdog -U
watchmedo shell-command --patterns="*.py;*.html;*.css;*.js" --recursive --command='echo "${watch_src_path}" && kill -HUP `cat gunicorn.pid`' . &
python manage.py run_gunicorn 127.0.0.1:80 --pid=gunicorn.pid
ANSWER 4
Score 7
I use git push to deploy to production and set up git hooks to run a script. The advantage of this approach is you can also do your migration and package installation at the same time. https://mikeeverhart.net/2013/01/using-git-to-deploy-code/
mkdir -p /home/git/project_name.git
cd /home/git/project_name.git
git init --bare
Then create a script /home/git/project_name.git/hooks/post-receive.
#!/bin/bash
GIT_WORK_TREE=/path/to/project git checkout -f
source /path/to/virtualenv/activate
pip install -r /path/to/project/requirements.txt
python /path/to/project/manage.py migrate
sudo supervisorctl restart project_name
Make sure to chmod u+x post-receive, and add user to sudoers. Allow it to run sudo supervisorctl without password. https://www.cyberciti.biz/faq/linux-unix-running-sudo-command-without-a-password/
From my local / development server, I set up git remote that allows me to push to the production server
git remote add production ssh://user_name@production-server/home/git/project_name.git
# initial push
git push production +master:refs/heads/master
# subsequent push
git push production master
As a bonus, you will get to see all the prompts as the script is running. So you will see if there is any issue with the migration/package installation/supervisor restart.