Python gunicorn 在源更改时自动重新加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12773763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:51:25  来源:igfitidea点击:

gunicorn autoreload on source change

pythondjangoreloadgunicorn

提问by Paolo

Finally I migrated my development env from runserver to gunicorn/nginx.

最后,我将我的开发环境从 runserver 迁移到了 gunicorn/nginx。

It'd be convenient to replicate the autoreload feature of runserver to gunicorn, so the server automatically restarts when source changes. Otherwise I have to restart the server manually with kill -HUP.

将 runserver 的 autoreload 功能复制到 gunicorn 会很方便,因此服务器会在源更改时自动重新启动。否则我必须手动重新启动服务器kill -HUP

Any way to avoid the manual restart?

有什么办法可以避免手动重启?

采纳答案by Dmitry Ziolkovskiy

While this is old question, just for consistency - since version 19.0 gunicorn has --reloadoption. So no third party tools needed more.

虽然这是个老问题,但只是为了一致性 - 因为版本 19.0 gunicorn 有--reload选项。所以没有第三方工具需要更多。

回答by Dave Forgac

One option would be to use the --max-requeststo limit each spawned process to serving only one request by adding --max-requests 1to 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.

一种选择是使用--max-requests通过添加--max-requests 1到启动选项来限制每个产生的进程只提供一个请求。每个新生成的进程都应该看到您的代码更改,并且在开发环境中,每个请求的额外启动时间应该可以忽略不计。

回答by hobs

Bryan Helmigcame up with this and I modified it to use run_gunicorninstead of launching gunicorndirectly, 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):

Bryan Helmig想出了这个,我修改了它以使用run_gunicorn而不是gunicorn直接启动,以便可以将这 3 个命令剪切并粘贴到 django 项目根文件夹中的 shell 中(激活 virtualenv):

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

回答by user3628119

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/

我使用 git push 部署到生产环境并设置 git hooks 来运行脚本。这种方法的优点是您还可以同时进行迁移和软件包安装。 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.

然后创建一个脚本/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 supervisorctlwithout password. https://www.cyberciti.biz/faq/linux-unix-running-sudo-command-without-a-password/

确保chmod u+x post-receive将用户添加到 sudoers。允许它在sudo supervisorctl没有密码的情况下运行。 https://www.cyberciti.biz/faq/linux-unix-running-sudo-command-without-a-password/

From my local / development server, I set up git remotethat allows me to push to the production server

从我的本地/开发服务器,我设置git remote允许我推送到生产服务器

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.

作为奖励,您将在脚本运行时看到所有提示。所以你会看到迁移/包安装/主管重启是否有任何问题。