推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lengyue233
V2EX  ›  Python

Python API 框架, 内置文档生成, 参数检验

  •  3
     
  •   lengyue233 ·
    leng-yue · Nov 9, 2021 · 2992 views
    This topic created in 1647 days ago, the information mentioned may be changed or developed.

    推一个类似 FastAPI 的 WSGI API 框架: HintAPI, 有以下特性

    • 支持参数校验 (基于 Pydantic)
    • 支持 OpenAPI 生成
    • 支持 Gevent 等有栈协程框架
    • 使用 RadixTree
    • 使用同步编写你的代码, 降低心智负担

    例子

    from hintapi import HintAPI
    from hintapi.openapi import OpenAPI
    
    app = HintAPI()
    
    app.router << ("/docs" // OpenAPI().routes)
    
    @app.router.http.patch("/{user_id}/name")
    def update_name(
        user_id: UUID = Path(),
        name: str = Body(),
    ):
    	...
    
    

    -- 初衷 --

    我朋友觉得 FastAPI 修 Bug 的速度太慢了, 开发也不热情, 所以他自己实现了一套类似的 ASGI (Async) API 框架, Index.py

    过了一两年到现在, Index.py 已经很稳定了, 但是 Python 的 Async 生态还是半死不活的, 加上数据库速度还没同步 + Gevent 快 😅. 所以他开发了 Index.py 的同步版本 HintAPI.

    6 replies    2021-11-09 10:04:56 +08:00
    szxczyc
        1
    szxczyc  
       Nov 9, 2021 via iPhone
    速度和 gin 比怎么样
    lengyue233
        2
    lengyue233  
    OP
       Nov 9, 2021
    @szxczyc Python 下的 API 框架, 应该没有和 Go 掰手腕的可能...
    ericls
        3
    ericls  
       Nov 9, 2021 via iPhone
    @szxczyc Python web 速度一般取决于服务器 而不是框架 框架都是对 wsgi 和 asgi 的包装
    abersheeran
        4
    abersheeran  
       Nov 9, 2021   ❤️ 1
    @szxczyc 实际业务里比不过 Gin ,但是应该可以比所有 ASGI 实现都快。毕竟 Gevent 等一众有栈协程的性能,远比现在半死不活的 asyncio 要好。举几个例子,aiomysql 是多线程改造的、motor 多线程改造的、aioredis 不支持集群实际用起来还得 redis 多线程改造。唯一好一点的就是 asyncpg ,但是不支持 DBAPI ,没有对应的 ORM 可用。

    我现在是 ASGI 和 WSGI 混着用。普通业务用同步,生态齐全、性能不差。一些极端业务必须要用 asyncio 的,就上 ASGI 。
    4BVL25L90W260T9U
        5
    4BVL25L90W260T9U  
       Nov 9, 2021
    牛逼啊,FastAPI 到现在都没有 API 文档,太坑了
    Fizzyi
        6
    Fizzyi  
       Nov 9, 2021
    惊现冷月大佬
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   967 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 19:59 · PVG 03:59 · LAX 12:59 · JFK 15:59
    ♥ Do have faith in what you're doing.