极速Web框架Robyn
作者:YXN-python 阅读量:13 发布日期:2024-10-02
性能测试
用wrk对Robyn和其他几个Python Web框架做了个简单的性能测试(Hello World接口,并发100,持续30秒):
- Robyn:45,000 请求/秒
- FastAPI:25,000 请求/秒
- Flask:8,000 请求/秒
安装
pip install robyn
基本应用
from robyn import Robyn
app = Robyn(__file__)
@app.get("/")
async def hello():
return "Hello, Robyn!"
if __name__ == "__main__":
app.start(port=5000)
处理json数据
from robyn import Robyn
from dataclasses import dataclass
from typing import Optional
app = Robyn(__file__)
@dataclass
class User:
name: str
age: Optional[int] = None
@app.post("/users")
@app.get("/users")
async def create_user(request):
data = request.json()
user = User(**data)
return {
"message": f"创建用户成功:{user.name}",
"user": data
}
if __name__ == "__main__":
app.start(port=5000)
请求
import requests
url = 'http://localhost:5000/users'
headers = {'Content-Type': 'application/json'}
data = {'name': 'John Doe', 'age': 30}
response = requests.post(url, json=data, headers=headers)
print(response.text)
YXN-python
2024-10-02