sanic项目框架架构及源码

github项目链接地址: https://github.com/Rgcsh/sanic_frame_demo

 

如下简要介绍一下 python的sanic的项目框架部分文档,此项目框架 基本开箱即用,省去 项目框架搭建的时间

# 项目概述
此项目主要目标是 记录sanic的项目框架,如何扩展第三方插件,加入中间件,蓝图管理等等,基本是一个拿来即用便可上线的项目框架

# 项目涉及到的插件
* redis使用sanic-redis包

#### 软件架构

|--sanic_frame_demo
    |-- app
    |   |-- __init__.py
    |    |-- middleware # 中间件(日志中间件)
    |    |-- core # 各种第三方插件 通过 init_app() 注册的方式 都集中此处
    |    |-- utils # 各种工具函数
    |    |    |-- constant.py # 常量
    |    |    |-- exception.py # 异常处理
    |    |    |-- ...
    |    |-- controllers # 路由模块
    |    |    |-- blueprint_api # 某个蓝图模块
    |    |    |-- ...
    |-- config # 项目配置层
    |    |-- __init__.py # 根据环境变量获取具体配置
    |    |-- base.py
    |    |-- local.py # 本地开发配置
    |    |-- develop.py # 测试服配置
    |    |-- product.py # 生产服配置
    |-- start.py # 项目启动文件
    |--    README.md
    |-- requirements.txt
```
# 项目部署或启动方法
* 环境变量设置
```shell script
export SANIC_ENV='LOCAl'
```
* 使用gunicorn运行,命令如下
```shell script
gunicorn -w 9 -b 0.0.0.0:5000 -t 10 start:app --worker-class sanic.worker.GunicornWorker
```
* 使用内置run()多进程运行,可以指定workers进程数量,看了源代码,好像此方法默认就是使用的uvloop,在sanic.server.py的35,36行
```shell script
python start.py
```
* 使用uvloop运行
```shell script
python start_loop.py
```

你可能感兴趣的:(sanic项目框架架构及源码)