问题:更改源代码时自动加载gunicorn

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

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

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

Finally I migrated my development env from runserver to 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.

Any way to avoid the manual restart?


回答 0

尽管这是个老问题,但仅出于一致性考虑-因为19.0版本的gunicorn可以--reload选择。因此,不再需要第三方工具。

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.


回答 1

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

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.


回答 2

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

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

回答 3

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

然后创建一个脚本/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

确保为chmod u+x post-receive,并将用户添加到sudoers。允许它sudo supervisorctl不带密码运行。 https://www.cyberciti.biz/faq/linux-unix-running-sudo-command-without-a-password/

在本地/开发服务器上,我进行了设置,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

另外,在脚本运行时,您将看到所有提示。因此,您将看到迁移/软件包安装/主管重启是否存在任何问题。

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。