• 【Flask+gunicorn+ supervisor】部署python项目


    一般情况下,部署python项目直接执行 python xxx.py 即可,不过这样部署非常不成熟。
    存在以下弊端:
    1、性能非常一般
    2、不方便记录日志
    3、不能后台运行

    推荐使用 pipenv

    # Pipfile
    jinja2 = "==3.0.3"
    gunicorn = "==20.1.0"
    supervisor = "==4.2.4"
    Werkzeug = "~=2.0.0"
    bcrypt = "==3.1.7"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    gunicorn

    为什么要用 gunicorn

    gunicorn一个被广泛使用的高性能的python WSGI UNIX HTTP服务器,Gunicorn是主流的WSGI容器之一,它易于配置,兼容性好,CPU消耗很少.

    gunicorn 命令

    -c CONFIG, --config=CONFIG
    # 设定配置文件。
    -b BIND, --bind=BIND
    # 设定服务需要绑定的端口。建议使用HOST:PORT
    -w WORKERS, --workers=WORKERS
    # 设置工作进程数。建议服务器每一个核心可以设置2-4个。
    -k MODULE
    # 选定异步工作方式使用的模块。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    运行命令:
    pipenv run gunicorn --workers=4 --bind=0.0.0.0:62323 wsgi:app
    wsgi:app -> 执行文件:入口函数(本来我是app.py,但是执行有些问题)
    
    • 1
    • 2
    • 3

    supervisor

    supervisor是一个python开发的进程管理工具,不光可以管理python项目,java项目,php-pfm后台启动程序都可以管理哦
    执行命令:

    # 加载一个 config模版,然后把下边这个配置复制过去即可
    pipenv run supervisord -c supervisor.conf
    # 修改配置
    supervisorctl -c ./supervisor.conf status
    supervisorctl -c ./supervisor.conf start all
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    supervisor.conf 添加配置

    [program:{package_name}]
    directory = %(here)s
    command = pipenv run gunicorn --workers=4 --bind=0.0.0.0:62323 wsgi:app
    autostart = true
    startsecs = 5
    autorestart = true
    startretries = 3
    redirect_stderr = true
    stdout_logfile_maxbytes = 200000000
    stdout_logfile_backups = 5
    stdout_logfile = %(here)s/wsgi.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    执行 supervisorctl -c ./supervisor.conf status 查看运行状态
    在这里插入图片描述
    如果成功运行了,就是这样,可以查看 wsgi.log的执行日志查看具体细节。

  • 相关阅读:
    tomcat之高并发配置Server.xml
    计算机毕业设计Java毕业设计流程管理(源码+系统+mysql数据库+lw文档)
    JS中数值类型的本质
    Wpf 使用 Prism 实战开发Day21
    idea中误删.iml和.idea文件,如何处理
    Node.js——http模块和导出共享
    Telnet弱口令渗透测试
    88Q2110 通过C22方式访问C45 phy地址
    GO 语言的函数??
    合成复用原则~
  • 原文地址:https://blog.csdn.net/qq_41902618/article/details/125481686