周凯,个人博客

  • 前端
  • 嵌入式
  • 工具
  • 后端
  • 随笔
个人记录
  1. 首页
  2. python
  3. 正文

python 生产环境部署脚本

2023年 10月 8日 1065点热度 0人点赞 0条评论
  • 根目录创建python文件

  • gunicorn.py

    import multiprocessing
    import os
    
    # 设置守护进程
    daemon = True
    # 监听内网端口8000
    bind = '0.0.0.0:8000'
    # 设置进程文件目录
    pidfile = './gunicorn.pid'
    chdir = './'  # 工作目录
    # 工作模式
    worker_class = 'uvicorn.workers.UvicornWorker'
    # 并行工作进程数 核心数*2+1个
    workers = multiprocessing.cpu_count() + 1
    # 指定每个工作者的线程数
    threads = 2
    # 设置最大并发量
    worker_connections = 2000
    loglevel = 'debug'  # 错误日志的日志级别
    access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
    # 设置访问日志和错误信息日志路径
    log_dir = "./logs"
    if not os.path.exists(log_dir):
      os.makedirs(log_dir)
    accesslog = "./logs/gunicorn_access.log"
    errorlog = "./logs/gunicorn_error.log"
  • 根目录创建 start.sh 启动脚本

    gunicorn main:app -c gunicorn.py
  • 根目录创建 stop.sh 停止脚本

    #! /bin/bash
    port=8000
    kill -9 "lsof -ti:$port"    # 使用的时候删除掉 "" 双引号,命令需要使用``两个反斜杠替换双引号
  • 根目录创建 showlog.sh 查看日志脚本

    #!/bin/bash
    
    function access () {
    tail -f ./logs/gunicorn_access.log
    }
    function error(){
    tail -f ./logs/gunicorn_error.log
    }
    
    if [ "$1" = "" ];
    then
      echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {access|error} \033[0m"
      exit 1
    fi
    
    case $1 in
      access)
      access;;
      error)
      error;;
    
    esac

参考

  • FastAPI部署在云服务器详细教程 - 知乎 (zhihu.com)

🎯 拓展阅读提示

本文涉及的内容已同步至公众号后台,我会在那里分享更多深度内容和实用技巧

→ 点击关注:一行梦境

公众号二维码
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2023年 10月 8日

周凯

这个人很懒,什么都没留下

打赏 点赞
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022-现在 周凯,个人博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蒙ICP备18004897号