Pydantic&FastAPI

陶修洁
2023-12-01

Pydantic

  • what is:
    Define how data should be in pure, canonical python; validate it with pydantic

官网: https://pydantic-docs.helpmanual.io/

Sample:

from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: Optional[datetime] = None
    friends: List[int] = []
   
external_data = {
    'id': '123',
    'signup_ts': '2019-06-01 12:22',
    'friends': [1, 2, '3'],
}
user = User(**external_data)
print(user.id)
#> 123
print(repr(user.signup_ts))
#> datetime.datetime(2019, 6, 1, 12, 22)
print(user.friends)
#> [1, 2, 3]
print(user.dict())
"""
{
    'id': 123,
    'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),
    'friends': [1, 2, 3],
    'name': 'John Doe',
}
"""

ref: https://blog.csdn.net/codename_cys/article/details/107675748
对于python2, 还有另一个库可以使用: marshmallow

FastAPI

  • why
  • https://www.jianshu.com/p/d01d3f25a2af

starlette

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.

It is production-ready, and gives you the following:

  • Seriously impressive performance.
  • WebSocket support.
  • In-process background tasks.
  • Startup and shutdown events.
  • Test client built on requests.
  • CORS, GZip, Static Files, Streaming responses.
  • Session and Cookie support.
  • 100% test coverage.
  • 100% type annotated codebase.
  • Few hard dependencies.

官网:https://www.starlette.io/

uvicorn

Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools.

官网: http://www.uvicorn.org/

ref: 异步服务器Uvicorn

  • 为什么还需要用 gunicorn ?
    官网的解释:

Using a process manager

  • Running Uvicorn using a process manager ensures that you can run multiple processes in a resilient manner, and allows you to perform server upgrades without dropping requests.

  • A process manager will handle the socket setup, start-up multiple server processes, monitor process aliveness, and listen for signals to provide for processes restarts, shutdowns, or dialing up and down the number of running processes.

  • Uvicorn provides a lightweight way to run multiple worker processes, for example --workers 4, but does not provide any process monitoring.

Gunicorn

  • Gunicorn is probably the simplest way to run and manage Uvicorn in a production setting. Uvicorn includes a gunicorn worker class that means you can get set up with very little configuration.
    The following will start Gunicorn with four worker processes:
 gunicorn -w 4 -k uvicorn.workers.UvicornWorker

Gunicorn vs Unicorn

  • Developers describe Gunicorn as “A Python WSGI HTTP Server for UNIX”.
  • Unicorn is detailed as “Rack HTTP server for fast clients and Unix”.

ref: https://stackshare.io/stackups/gunicorn-vs-unicorn

pyre

pyre is an async server written in Rust that gives big benchmark performance increases but minimal real-world improvements so take benchmarks as a grain of salt and respect the performance existing servers give.
ref: https://www.reddit.com/r/rust/comments/ob4kli/12_months_of_rust_50k_changed_lines_of_code_8/

Hydra

Hydra aims to provide a unviseral fit for WSGI/ASGI or any other interface for frameworks through a mutual connection while also boosting performance and adding the ability for stateful worker processes

https://github.com/Project-Dream-Weaver/Hydra

 类似资料: