Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • python 学习指南
  • python 中的设计模式
  • module

    • python modules
      • uvicorn
      • click
      • argparse
      • pypinyin
      • logging
      • link
    • pandas
    • numpy
    • matplotlib
    • scipy
    • python typing 模块
    • inspect
    • beautysoup
    • scrapy
    • splash
    • pytorch
  • tool

  • other

  • 《python》
  • module
Jacky
2024-02-19
目录

python modules

# uvicorn

uvicorn (opens new window) 是一个轻量级、超快速的 ASGI (Asynchronous Server Gateway Interface) 服务器实现, 基于 uvloop 和 httptools 构建

Uvicorn 是运行异步 Python Web 应用的首选服务器, 特别适合 FastAPI、Starlette 等现代异步框架

主要特性
  • 高性能: 基于 uvloop (libuv 的 Python 实现) 和 httptools, 提供极高的性能
  • ASGI 支持: 完整支持 ASGI 3.0 规范, 可运行异步 Python Web 应用
  • WebSocket 支持: 原生支持 WebSocket 协议
  • 自动重载: 开发模式下支持代码修改后自动重启
  • 生产就绪: 支持多进程、优雅关闭等生产环境特性
  • HTTPS 支持: 可配置 SSL/TLS 证书
安装
# 标准安装
pip install uvicorn

# 安装标准版本(包含性能优化依赖)
pip install uvicorn[standard]
1
2
3
4
5
基本使用
# main.py
async def app(scope, receive, send):
    assert scope['type'] == 'http'
    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [[b'content-type', b'text/plain']],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })

# 运行命令
# uvicorn main:app --reload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

与 FastAPI 配合使用

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

# 运行: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
1
2
3
4
5
6
7
8
9
常用命令行参数
  • --host: 绑定的主机地址 (默认: 127.0.0.1)
  • --port: 绑定的端口 (默认: 8000)
  • --reload: 开启自动重载 (开发模式)
  • --workers: 工作进程数量 (生产模式)
  • --log-level: 日志级别 (debug, info, warning, error, critical)
  • --access-log / --no-access-log: 是否记录访问日志
  • --ssl-keyfile: SSL 密钥文件路径
  • --ssl-certfile: SSL 证书文件路径
代码中启动
import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}

if __name__ == "__main__":
    # 开发模式
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )
    
    # 生产模式(多进程)
    # uvicorn.run(
    #     app,
    #     host="0.0.0.0",
    #     port=8000,
    #     workers=4,
    #     log_level="warning"
    # )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
与 Gunicorn 配合(生产环境推荐)
# 安装
pip install gunicorn uvicorn[standard]

# 使用 Gunicorn 管理多个 Uvicorn 工作进程
gunicorn main:app \
    --workers 4 \
    --worker-class uvicorn.workers.UvicornWorker \
    --bind 0.0.0.0:8000 \
    --access-logfile - \
    --error-logfile -
1
2
3
4
5
6
7
8
9
10
性能优化建议
  1. 安装标准版本: pip install uvicorn[standard] (包含 uvloop 和 httptools)
  2. 生产环境使用多进程: --workers 参数设置为 CPU 核心数
  3. 关闭开发模式的 --reload
  4. 合理设置日志级别: 生产环境使用 warning 或 error
  5. 使用 Gunicorn 作为进程管理器

# click

click (opens new window) 模块是一个用于创建命令行界面(CLI)的库, 它提供了简单而强大的方法来定义命令行参数、选项和子命令, 并且可以自动生成帮助文档

使用 Click, 你可以轻松地将 Python 函数转换为命令行命令, 并为这些命令添加参数、选项和子命令, 从而创建一个功能强大且易于使用的命令行界面

click 模块的一些主要特性
  • 装饰器风格的命令定义: 通过装饰器可以将 Python 函数转换为命令行命令, 并指定命令的名称、参数、选项等信息

  • 参数和选项支持: 支持定义命令的参数和选项, 包括位置参数、可选参数、flag 参数等

  • 多命令支持: 支持定义包含多个子命令的命令集合, 使得可以构建复杂的命令行界面

  • 自动生成帮助文档: 根据命令和参数的定义, Click 可以自动生成帮助文档, 包括命令的使用说明、参数的说明、示例等

  • 类型转换和验证: 支持在定义参数时指定参数的类型, 并且可以进行类型转换和验证

  • 命令行自动补全: Click 支持在命令行中使用 Tab 键进行命令、参数和选项的自动补全

  • 支持多种输出格式: 可以根据需要将输出结果格式化为文本、JSON 等格式

# argparse

argparse是Python标准库中用于解析命令行参数的模块,可以轻松创建用户友好的命令行界面。

# pypinyin

# logging

import logging

dispatcher = logging.getLogger("aiogram.dispatcher")
event = logging.getLogger("aiogram.event")
middlewares = logging.getLogger("aiogram.middlewares")
webhook = logging.getLogger("aiogram.webhook")
scene = logging.getLogger("aiogram.scene")
1
2
3
4
5
6
7

# link

  • Find, install and publish Python packages with the Python Package Index (opens new window)
#module#python
上次更新: 2025/11/02, 21:28:37
python 中的设计模式
pandas

← python 中的设计模式 pandas→

最近更新
01
math经典公式
11-19
02
线性代数
11-18
03
电商运营核心指标图表
11-16
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式