141

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

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

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

4

4 回答 4

287

虽然这是一个老问题,但您需要知道,自从 19.0 版gunicorn以来,就有了这个--reload选项。所以现在不需要第三方工具。

于 2014-07-22T16:40:00.683 回答
21

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

于 2013-06-13T04:30:54.350 回答
15

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
于 2013-10-21T19:24:55.497 回答
7

我使用 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

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

于 2018-08-15T14:17:09.073 回答