V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2021-11-09 03:23:51 +08:00 · 2207 次点击
    这是一个创建于 892 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    我现在是 ASGI 和 WSGI 混着用。普通业务用同步,生态齐全、性能不差。一些极端业务必须要用 asyncio 的,就上 ASGI 。
    ospider
        5
    ospider  
       2021-11-09 09:30:59 +08:00
    牛逼啊,FastAPI 到现在都没有 API 文档,太坑了
    Fizzyi
        6
    Fizzyi  
       2021-11-09 10:04:56 +08:00
    惊现冷月大佬
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1093 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:38 · PVG 02:38 · LAX 11:38 · JFK 14:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.