-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
02-online serving/examples를 최신화 (#168)
* refactor: pyproject.toml의 파이썬과 라이브러리 버전을 업데이트 * chore: 사용하지 않는 파일 삭제 * refactor: 11_event_handler.py 예제를 최신 fastapi 스타일로 변경 * chore: log.txt 추가
- Loading branch information
Showing
3 changed files
with
503 additions
and
618 deletions.
There are no files selected for viewing
16 changes: 8 additions & 8 deletions
16
...ing(fastapi)/examples/11_event_handler.py → ...-serving(fastapi)/examples/11_lifespan.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
from contextlib import asynccontextmanager | ||
|
||
from fastapi import FastAPI | ||
import uvicorn | ||
|
||
app = FastAPI() | ||
|
||
items = {} | ||
|
||
|
||
@app.on_event("startup") | ||
def startup_event(): | ||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
print("Start Up Event") | ||
items["foo"] = {"name": "Fighters"} | ||
items["bar"] = {"name": "Tenders"} | ||
|
||
yield | ||
|
||
@app.on_event("shutdown") | ||
def shutdown_event(): | ||
print("Shutdown Event!") | ||
with open("log.txt", mode="a") as log: | ||
log.write("Application shutdown") | ||
|
||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
|
||
@app.get("/items/{item_id}") | ||
def read_items(item_id: str): | ||
return items[item_id] | ||
|
||
|
||
if __name__ == '__main__': | ||
uvicorn.run(app, host="0.0.0.0", port=8000) | ||
|
||
|
Oops, something went wrong.