本文介绍了如何优雅地重新启动在 nginx 后面运行 fcgi 的 django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 fcgi 连接的 nginx 后面运行 django 实例(通过使用 manage.py runfcgi 命令).由于代码已加载到内存中,因此我无法在不杀死并重新启动 django fcgi 进程的情况下重新加载新代码,从而中断实时网站.重新启动本身非常快.但是通过首先杀死 fcgi 进程,一些用户的操作会被中断,这是不好的.我想知道如何在不造成任何中断的情况下重新加载新代码.建议将不胜感激!

I'm running a django instance behind nginx connected using fcgi (by using the manage.py runfcgi command). Since the code is loaded into memory I can't reload new code without killing and restarting the django fcgi processes, thus interrupting the live website. The restarting itself is very fast. But by killing the fcgi processes first some users' actions will get interrupted which is not good.I'm wondering how can I reload new code without ever causing any interruption. Advices will be highly appreciated!

推荐答案

我会在新端口上启动一个新的 fcgi 进程,更改 nginx 配置以使用新端口,让 nginx 重新加载配置(这本身很优雅),然后最终停止旧进程(您可以使用 netstat 找出与旧端口的最后一个连接何时关闭).

I would start a new fcgi process on a new port, change the nginx configuration to use the new port, have nginx reload configuration (which in itself is graceful), then eventually stop the old process (you can use netstat to find out when the last connection to the old port is closed).

或者,您可以更改 fcgi 实现以 fork 一个新进程,关闭子进程中除 fcgi 服务器套接字之外的所有套接字,关闭父进程中的 fcgi 服务器套接字,在子进程中执行一个新的 django 进程(使其使用fcgi 服务器套接字),并在所有 fcgi 连接关闭后终止父进程.IOW,为runfcgi实现优雅重启.

Alternatively, you can change the fcgi implementation to fork a new process, close all sockets in the child except for the fcgi server socket, close the fcgi server socket in parent, exec a new django process in the child (making it use the fcgi server socket), and terminate the parent process once all fcgi connections are closed. IOW, implement graceful restart for runfcgi.

这篇关于如何优雅地重新启动在 nginx 后面运行 fcgi 的 django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 02:12