官网: 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
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:
官网:https://www.starlette.io/
Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools.
官网: http://www.uvicorn.org/
ref: 异步服务器Uvicorn
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
ref: https://stackshare.io/stackups/gunicorn-vs-unicorn
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 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