01、uwsgi、gunicorn如何实现优雅重启

yizhihongxing

1、为何需要优雅重启

在实际开发过程中,我们会不断迭代升级产品,每次迭代后,都需要在线上服务器更新代码。一般小公司的迭代升级,是没有做到像金丝雀发布或者使用到kubernetes这些东西的。那如何保证更新的时候,之前接收到的请求能够正常处理完成呢,这个时候就需要实现优雅重启了。

那如何实现优雅重启呢,其实,我们部署python web服务所用到的uwsgi和gunicorn已经实现了优雅重启了,下面就讲讲如何实现优雅重启

2、uwsgi 如何实现优雅重启

以下实验是基于以下版本进行的。

python3.6.8

flask==2.0.3
uwsgi==2.0.21

2.1 编写 web 服务

main.py

import time

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    time.sleep(10)
    return "hello eeee"


if __name__ == "__main__":
    app.run()

2.2 编写 uwsgi.ini 配置文件

[uwsgi]
#uwsgi启动时,所使用的地址和端口(这个是http协议的)
http=0.0.0.0:8000
#指向网站目录
chdir=./
#python 启动程序文件
wsgi-file=main.py
#python 程序内用以启动的application 变量名
callable=app
#处理器数
processes=4
#线程数
threads=2

#####实现优雅重启添加下面两行配置即刻#####
lazy-apps = true
#监听 test.txt 文件 当 test.txt 发生改变时优雅重启uwsgi。这个名字可以随便起
touch-chain-reload = /Users/xx/work/test/py_test/sample_test/flask_graceful_restart/test.txt

2.3 启动uwsgi 服务

uwsgi --ini uwsgi.ini

2.4 测试优雅重启

1、请求 http://127.0.0.1:8000/

2、更新 main.py 中返回的内容,改为: return "hello xxxxx"

2、在/Users/xx/work/test/py_test/sample_test/flask_graceful_restart 目录下,执行 touch test.txt。有这个文件时,更改这个文件的内容也可以优雅重启 uwsgi 服务

3、得到第一步的返回结果,返回结果为:"hello eeee"

5、再次请求 http://127.0.0.1:8000/ ,返回结果为:"hello xxxxx"

通过上述测试,可以发现实现了优雅重启。

优雅重启的日志过程:

整个时间还挺久的,差不多4分钟。

开始:12:14:45。

结束:12:18:57。

1、先查看 uwsgi 进程信息

501  7758  4633   0 12:13PM ttys005    0:00.04 uwsgi --ini uwsgi.ini
501  7759  7758   0 12:13PM ttys005    0:00.27 uwsgi --ini uwsgi.ini
501  7760  7758   0 12:13PM ttys005    0:00.27 uwsgi --ini uwsgi.ini
501  7761  7758   0 12:13PM ttys005    0:00.27 uwsgi --ini uwsgi.ini
501  7762  7758   0 12:13PM ttys005    0:00.26 uwsgi --ini uwsgi.ini
501  7763  7758   0 12:13PM ttys005    0:00.00 uwsgi --ini uwsgi.ini
501  7789  6013   0 12:13PM ttys006    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox uwsgi

2、生成 test.txt 时的现象:当 出现 chain reloading complete 时,代表了优雅完成。

Mon Apr 10 12:14:45 2023 - *** /Users/mashili/work/test/py_test/sample_test/flask_graceful_restart/test.txt has been touched... chain reload !!! ***
Mon Apr 10 12:14:45 2023 - chain next victim is worker 1
Gracefully killing worker 1 (pid: 7759)...
Mon Apr 10 12:15:46 2023 - worker 1 (pid: 7759) is taking too much time to die...NO MERCY !!!
worker 1 killed successfully (pid: 7759)
Respawned uWSGI worker 1 (new pid: 7847)
Mon Apr 10 12:15:47 2023 - chain is still waiting for worker 1...
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fc33e00da00 pid: 7847 (default app)
Mon Apr 10 12:15:48 2023 - chain next victim is worker 2
Gracefully killing worker 2 (pid: 7760)...
Mon Apr 10 12:16:49 2023 - worker 2 (pid: 7760) is taking too much time to die...NO MERCY !!!
worker 2 killed successfully (pid: 7760)
Respawned uWSGI worker 2 (new pid: 7885)
Mon Apr 10 12:16:50 2023 - chain is still waiting for worker 2...
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fc33e00da00 pid: 7885 (default app)
Mon Apr 10 12:16:51 2023 - chain next victim is worker 3
Gracefully killing worker 3 (pid: 7761)...
Mon Apr 10 12:17:52 2023 - worker 3 (pid: 7761) is taking too much time to die...NO MERCY !!!
worker 3 killed successfully (pid: 7761)
Respawned uWSGI worker 3 (new pid: 7905)
Mon Apr 10 12:17:53 2023 - chain is still waiting for worker 3...
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fc33e00da00 pid: 7905 (default app)
Mon Apr 10 12:17:54 2023 - chain next victim is worker 4
Gracefully killing worker 4 (pid: 7762)...
Mon Apr 10 12:18:55 2023 - worker 4 (pid: 7762) is taking too much time to die...NO MERCY !!!
worker 4 killed successfully (pid: 7762)
Respawned uWSGI worker 4 (new pid: 7910)
Mon Apr 10 12:18:56 2023 - chain is still waiting for worker 4...
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fc33e00da00 pid: 7910 (default app)
Mon Apr 10 12:18:57 2023 - chain reloading complete

3、优雅重启过程中,查看进程信息。ps -ef|grep uwsgi

发现即存在新的进程,也存在老的进程。测试的时候,发现,优雅重启过程中,并不一定会将重启过程中的请求转发到新的进程中去。

  501  7758  4633   0 12:13PM ttys005    0:00.08 uwsgi --ini uwsgi.ini
  501  7761  7758   0 12:13PM ttys005    0:00.27 uwsgi --ini uwsgi.ini
  501  7762  7758   0 12:13PM ttys005    0:00.26 uwsgi --ini uwsgi.ini
  501  7763  7758   0 12:13PM ttys005    0:00.00 uwsgi --ini uwsgi.ini
  501  7847  7758   0 12:15PM ttys005    0:00.26 uwsgi --ini uwsgi.ini
  501  7885  7758   0 12:16PM ttys005    0:00.26 uwsgi --ini uwsgi.ini
  501  7889  6013   0 12:17PM ttys006    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox uwsgi

2.5 线上如何更新

1、首先将代码更新到服务器。

2、ps -ef|grep uwsgi 查看现在的进程号。

2、查看 test.txt是否存在,存在就更新文件内容,不存在就生成 test.txt。

3、观察uwsgi的日志或者进程,待所有的worker进程都重启生成后,即完成了优雅重启。

3、gunicorn 如何实现优雅重启

3.1 编写 web 服务

main.py

import time

from flask import Flask

app = Flask(__name__)


@app.route("/")
def index():
    # time.sleep(3)
    return "hello fdaf fdafd "


if __name__ == "__main__":
    app.run()

3.2 编写 conf.py 配置gunicorn 文件

conf.py

# 是否开启debug模式
debug = True
# 访问地址
bind = "0.0.0.0:8888"
# 工作进程数
workers = 2
# 工作线程数
threads = 2
# 超时时间
timeout = 600
# 输出日志级别
loglevel = 'info'
# 存放日志路径
pidfile = "log/gunicorn.pid"
# 存放日志路径
accesslog = "log/access.log"
# 存放日志路径
errorlog = "log/debug.log"

######注意,下面这个不能加,加了就不能达到优雅重启的效果,切记切记!!
# gunicorn + apscheduler场景下,解决多worker运行定时任务重复执行的问题
# preload_app = True

3.3 启动 gunicorn 服务

gunicorn -c conf.py main:app

3.4 测试优雅重启

1、pstree -ap|grep gunicorn 找到主进程

pstree -ap|grep gunicorn

2、执行 kill -HUP masterpid

kill -HUP 1540847

3、再次 执行 pstree -ap|grep gunicorn,发现worker 进程id不一样后,即更新完成。或者查看日志

[2023-04-10 15:36:51 +0800] [11623] [INFO] Handling signal: hup
[2023-04-10 15:36:51 +0800] [11623] [INFO] Hang up: Master
[2023-04-10 15:36:51 +0800] [11681] [INFO] Booting worker with pid: 11681
[2023-04-10 15:36:51 +0800] [11682] [INFO] Booting worker with pid: 11682
[2023-04-10 15:36:51 +0800] [11644] [INFO] Worker exiting (pid: 11644)
[2023-04-10 15:36:51 +0800] [11645] [INFO] Worker exiting (pid: 11645)

3.5 线上如何更新

1、通过 pstree -ap|grep gunicorn 找到主进程ID

2、执行 kill -HUP masterpid 命令

3、等待gunicorn优雅重启完成

原文链接:https://www.cnblogs.com/huageyiyangdewo/p/17303182.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:01、uwsgi、gunicorn如何实现优雅重启 - Python技术站

(0)
上一篇 2023年4月18日
下一篇 2023年4月18日

相关文章

  • 一篇文章带你了解python正则表达式的正确用法

    一篇文章带你了解Python正则表达式的正确用法 正则表达式是一种用于描述字符串模式的语言,可以用匹配、查找、替换和割字符串。Python中的re模块提供了正则表达式支持,方便进行字符串的处理。本文将详细讲解Python正则表达式使用,包括正则表达式语法、re模块的常用函数以及两个用匹配实例。 正则表达式语法 正则表达式由一些特殊字符和普通字符组成,用于字符…

    python 2023年5月14日
    00
  • Python requests模块实例用法

    以下是关于Python requests模块实例用法的攻略: Python requests模块实例用法 requests是Python中一个流行的HTTP库,可以用于向Web服务器发送HTTP请求和接收响应。以下是Python requests模块实例用法: 发送GET请求 以下是使用requests发送GET请求的示例: import requests …

    python 2023年5月14日
    00
  • 详解Python PIL Image.draft()方法

    Python PIL库中的Image.draft()方法用于将图像转换为“草稿”模式,并返回该图像。“草稿”格式的图像比原始格式的图像更快,但图像质量较差。该方法可以为图像的处理提供加速,特别是在处理大量图像时。下面是更详细的完整攻略: 什么是 Python PIL Image.draft()方法? Image.draft()方法是Python PIL库中的…

    python-answer 2023年3月25日
    00
  • Pytest单元测试框架生成HTML测试报告及优化的步骤

    Pytest是一个流行的Python单元测试框架,可以生成HTML测试报告。以下是Pytest单元测试框架生成HTML测试报告及优化的步骤的详细攻略: 安装pytest-html插件 要生成HTML测试报告,需要安装pytest-html插件。可以使用pip安装pytest-html插件。以下是安装pytest-html插件的示例: pip install …

    python 2023年5月14日
    00
  • Python自动发邮件脚本

    下面将为您详解”Python自动发邮件脚本”的完整攻略。 前置知识 在学习Python自动发邮件脚本之前,您需要掌握以下技能: Python基础语法 smtplib模块的基本使用 使用SMTP协议发送邮件的基本流程 发送邮件原理 发送邮件的原理是通过SMTP协议,将邮件服务器作为客户端连接到邮件服务器,并进行身份验证后,利用sendmail()方法,将邮件发…

    python 2023年5月19日
    00
  • Python爬虫技术

    Python爬虫技术 Python爬虫技术是通过编写程序,自动从互联网上爬取数据并进行处理分析的技术。Python作为一种功能强大、语法简洁、易于学习的编程语言,被广泛应用于爬虫领域。 爬虫的基本流程 1. 确定爬取的目标和方式 在开始爬虫的过程中,首先需要明确爬虫的目标和方式。需要明确爬取的数据类型、要爬取的网站、爬虫的频次等等。 2. 构造URL和请求 …

    python 2023年5月14日
    00
  • Python实现将16进制字符串转化为ascii字符的方法分析

    下面我将详细讲解“Python实现将16进制字符串转化为ascii字符的方法分析”的完整攻略。 1. 背景知识 在计算机中,16进制用来表示数字与字符,称为十六进制数。十六进制数由0-9和A-F(或a-f)组成,其中A-F(或a-f)分别表示10-15。在Python中,使用int函数可以将一个16进制字符串转化为整数。 2. 将16进制字符串转化为asci…

    python 2023年5月20日
    00
  • Python如何使用正则表达式爬取京东商品信息

    以下是详细讲解“Python如何使用正则表达式爬取京东商品信息”的完整攻略,包括爬取京东商品信息的基本流程、正则表达的基本语法、使用re模块匹配网页内容的方法和两个示例说明。 爬取京东商品信息的基本流程 爬取京东商品信息的基本流程如下: 发送HTTP请求,获取网页内容。 解析网页内容,提取商品信息。 保存商品信息。 正则表达式基本语法 正则表达式是一种用于匹…

    python 2023年5月14日
    00
合作推广
合作推广
分享本页
返回顶部