gunicorn autoreload on source change

Pao*_*olo 106 python django reload gunicorn

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

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

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

Dmi*_*kiy 211

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

  • 同意.其他答案可能有效,但这是迄今为止最简单的,而不是解决方法.这正是OP想要的. (5认同)
  • 谢谢@Guandalino,我一定错过了.有趣的是,他们说"这个设置是用于开发的".在某些情况下,这显然适用于生产,但对许多其他情况也可能存在问题.是的,我确实在下面看到您对生产/部署似乎不感兴趣. (3认同)
  • @SoFLy这个吗?http://docs.gunicorn.org/zh/19.0/settings.html#reload (2认同)
  • @juanIsaza 你永远不应该在生产中使用这样的功能。如果你认为你需要它 - 你需要重新考虑你的开发或部署方法。 (2认同)

Dav*_*gac 20

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

  • 启动新工作人员需要大约3秒钟,这对我来说太慢了.(2009年中期MBP) (2认同)

hob*_*obs 10

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
Run Code Online (Sandbox Code Playgroud)


use*_*119 7

我使用 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
Run Code Online (Sandbox Code Playgroud)

然后创建一个脚本/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
Run Code Online (Sandbox Code Playgroud)

确保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
Run Code Online (Sandbox Code Playgroud)

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