pip install fastapi
pip install "uvicorn[standard]"
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
or
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
uvicorn main:app --reload
app = FastAPI()
http://127.0.0.1:8000/items/5?q=somequery
http://127.0.0.1:8000/docs
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: Union[bool, None] = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
벤치 결과 순수하게 fastapi만 사용해서는 nodejs나 go만큼 나오지는 않더라구요. orjson + 비동기 orm + uvloop 사용시 nodejs보다 나은 성능을 보이고, 이 조건에서 gunicorn 워커 8개를 사용하면 go보다 약간 부족한 성능을 보입니다.