Skip to content

๐Ÿ”† ๐ŸŽ‰

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ โš› (๐Ÿ“Ÿ) ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ› ๏ธ โญ ๐Ÿˆธ โ–ถ๏ธ ๐Ÿ†™. ๐Ÿ‘‰ โ›“ ๐Ÿ‘ˆ ๐Ÿ‘‰ ๐Ÿ“Ÿ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ•, โญ ๐Ÿˆธ โ–ถ๏ธ ๐Ÿ“จ ๐Ÿ“จ.

๐ŸŽ ๐ŸŒŒ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ โš› (๐Ÿ“Ÿ) ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ•โ” ๐Ÿˆธ ๐Ÿคซ ๐Ÿ”ฝ. ๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ‘‰ ๐Ÿ“Ÿ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ•, โฎ๏ธ โœ”๏ธ ๐Ÿต ๐ŸŽฒ ๐Ÿ“š ๐Ÿ“จ.

โ†ฉ๏ธ ๐Ÿ‘‰ ๐Ÿ“Ÿ ๐Ÿ› ๏ธ โญ ๐Ÿˆธ โ–ถ๏ธ โœŠ ๐Ÿ“จ, & โ–ถ๏ธ๏ธ โฎ๏ธ โšซ๏ธ ๐Ÿ ๐Ÿšš ๐Ÿ“จ, โšซ๏ธ ๐Ÿ“” ๐ŸŽ‚ ๐Ÿˆธ ๐Ÿ”† (๐Ÿ”ค "๐Ÿ”†" ๐Ÿ”œ โš  ๐Ÿฅˆ ๐Ÿ‘ถ).

๐Ÿ‘‰ ๐Ÿ’ช ๐Ÿ“ถ โš  โš’ ๐Ÿ†™ โ„น ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ‚ ๐Ÿ“ฑ, & ๐Ÿ‘ˆ ๐Ÿ’ฐ ๐Ÿ‘ช ๐Ÿ“จ, &/โš–๏ธ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿงน ๐Ÿ†™ โฎ๏ธ. ๐Ÿ–ผ, ๐Ÿ’ฝ ๐Ÿ”— ๐ŸŽฑ, โš–๏ธ ๐Ÿšš ๐Ÿ”— ๐ŸŽฐ ๐Ÿซ ๐Ÿท.

โš™๏ธ ๐Ÿ’ผ

โžก๏ธ โ–ถ๏ธ โฎ๏ธ ๐Ÿ–ผ โš™๏ธ ๐Ÿ’ผ & โคด๏ธ ๐Ÿ‘€ โ” โŽ โšซ๏ธ โฎ๏ธ ๐Ÿ‘‰.

โžก๏ธ ๐ŸŒˆ ๐Ÿ‘ˆ ๐Ÿ‘† โœ”๏ธ ๐ŸŽฐ ๐Ÿซ ๐Ÿท ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’š โš™๏ธ ๐Ÿต ๐Ÿ“จ. ๐Ÿ‘ถ

๐ŸŽ ๐Ÿท ๐Ÿ”— ๐Ÿ‘ช ๐Ÿ“จ,, โšซ๏ธ ๐Ÿšซ 1๏ธโƒฃ ๐Ÿท ๐Ÿ“ ๐Ÿ“จ, โš–๏ธ 1๏ธโƒฃ ๐Ÿ“ ๐Ÿ‘ฉโ€๐Ÿ’ป โš–๏ธ ๐Ÿ•ณ ๐ŸŽ.

โžก๏ธ ๐ŸŒˆ ๐Ÿ‘ˆ ๐Ÿšš ๐Ÿท ๐Ÿ’ช โœŠ ๐Ÿ•ฐ, โ†ฉ๏ธ โšซ๏ธ โœ”๏ธ โœ ๐Ÿ“š ๐Ÿ’ฝ โšช๏ธโžก๏ธ ๐Ÿ’พ. ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’š โšซ๏ธ ๐Ÿ”  ๐Ÿ“จ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ โšซ๏ธ ๐Ÿ” ๐ŸŽš ๐Ÿ•น/๐Ÿ“, โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿ”œ โ›“ ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿ”œ ๐Ÿ“ ๐Ÿท ๐Ÿšฅ ๐Ÿ‘† ๐Ÿƒโ€โ™‚ ๐Ÿ™… ๐Ÿง ๐Ÿ’ฏ, โคด๏ธ ๐Ÿ‘ˆ ๐Ÿ’ฏ ๐Ÿ”œ ๐ŸŒ โ†ฉ๏ธ โšซ๏ธ ๐Ÿ”œ โœ”๏ธ โŒ› ๐Ÿท ๐Ÿ“ โญ ๐Ÿ’†โ€โ™‚ ๐Ÿ’ช ๐Ÿƒ ๐Ÿ”ฌ ๐Ÿ• ๐Ÿ“Ÿ.

๐Ÿ‘ˆ โšซ๏ธโ” ๐Ÿ‘ฅ ๐Ÿ”œ โŽ, โžก๏ธ ๐Ÿ“ ๐Ÿท โญ ๐Ÿ“จ ๐Ÿต, โœ‹๏ธ ๐Ÿ•ด โ–ถ๏ธ๏ธ โญ ๐Ÿˆธ โ–ถ๏ธ ๐Ÿ“จ ๐Ÿ“จ, ๐Ÿšซ โช ๐Ÿ“Ÿ โž– ๐Ÿ“.

๐Ÿ”†

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ‘‰ ๐Ÿ•ด & ๐Ÿคซ โš› โš™๏ธ lifespan ๐Ÿ”ข FastAPI ๐Ÿ“ฑ, & "๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ" (๐Ÿ‘ค ๐Ÿ”œ ๐ŸŽฆ ๐Ÿ‘† โšซ๏ธโ” ๐Ÿ‘ˆ ๐Ÿฅˆ).

โžก๏ธ โ–ถ๏ธ โฎ๏ธ ๐Ÿ–ผ & โคด๏ธ ๐Ÿ‘€ โšซ๏ธ โ„น.

๐Ÿ‘ฅ โœ ๐Ÿ” ๐Ÿ”ข lifespan() โฎ๏ธ yield ๐Ÿ’– ๐Ÿ‘‰:

from contextlib import asynccontextmanager

from fastapi import FastAPI


def fake_answer_to_everything_ml_model(x: float):
    return x * 42


ml_models = {}


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Load the ML model
    ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
    yield
    # Clean up the ML models and release the resources
    ml_models.clear()


app = FastAPI(lifespan=lifespan)


@app.get("/predict")
async def predict(x: float):
    result = ml_models["answer_to_everything"](x)
    return {"result": result}

๐Ÿ“ฅ ๐Ÿ‘ฅ โš– ๐Ÿ˜ฅ ๐Ÿ•ด ๐Ÿ› ๏ธ ๐Ÿšš ๐Ÿท ๐Ÿšฎ (โŒ) ๐Ÿท ๐Ÿ”ข ๐Ÿ“– โฎ๏ธ ๐ŸŽฐ ๐Ÿซ ๐Ÿท โญ yield. ๐Ÿ‘‰ ๐Ÿ“Ÿ ๐Ÿ”œ ๐Ÿ› ๏ธ โญ ๐Ÿˆธ โ–ถ๏ธ โœŠ ๐Ÿ“จ, โฎ๏ธ ๐Ÿ•ด.

& โคด๏ธ, โ–ถ๏ธ๏ธ โฎ๏ธ yield, ๐Ÿ‘ฅ ๐Ÿšš ๐Ÿท. ๐Ÿ‘‰ ๐Ÿ“Ÿ ๐Ÿ”œ ๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿˆธ ๐Ÿ ๐Ÿšš ๐Ÿ“จ, โ–ถ๏ธ๏ธ โญ ๐Ÿคซ. ๐Ÿ‘‰ ๐Ÿ’ช, ๐Ÿ–ผ, ๐Ÿš€ โ„น ๐Ÿ’– ๐Ÿ’พ โš–๏ธ ๐Ÿ’ป.

Tip

shutdown ๐Ÿ”œ ๐Ÿ”จ ๐Ÿ•โ” ๐Ÿ‘† โ›”๏ธ ๐Ÿˆธ.

๐ŸŽฒ ๐Ÿ‘† ๐Ÿ’ช โ–ถ๏ธ ๐Ÿ†• โฌ, โš–๏ธ ๐Ÿ‘† ๐Ÿคš ๐ŸŽก ๐Ÿƒ โšซ๏ธ. ๐Ÿคท

๐Ÿ”† ๐Ÿ”ข

๐Ÿฅ‡ ๐Ÿ‘œ ๐Ÿ‘€, ๐Ÿ‘ˆ ๐Ÿ‘ฅ โš– ๐Ÿ” ๐Ÿ”ข โฎ๏ธ yield. ๐Ÿ‘‰ ๐Ÿ“ถ ๐ŸŽ ๐Ÿ”— โฎ๏ธ yield.

from contextlib import asynccontextmanager

from fastapi import FastAPI


def fake_answer_to_everything_ml_model(x: float):
    return x * 42


ml_models = {}


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Load the ML model
    ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
    yield
    # Clean up the ML models and release the resources
    ml_models.clear()


app = FastAPI(lifespan=lifespan)


@app.get("/predict")
async def predict(x: float):
    result = ml_models["answer_to_everything"](x)
    return {"result": result}

๐Ÿฅ‡ ๐Ÿ• ๐Ÿ”ข, โญ yield, ๐Ÿ”œ ๐Ÿ› ๏ธ โญ ๐Ÿˆธ โ–ถ๏ธ.

& ๐Ÿ• โฎ๏ธ yield ๐Ÿ”œ ๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿˆธ โœ”๏ธ ๐Ÿ.

๐Ÿ” ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ

๐Ÿšฅ ๐Ÿ‘† โœ…, ๐Ÿ”ข ๐ŸŽ€ โฎ๏ธ @asynccontextmanager.

๐Ÿ‘ˆ ๐Ÿ—œ ๐Ÿ”ข ๐Ÿ”˜ ๐Ÿ•ณ ๐Ÿค™ "๐Ÿ” ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ".

from contextlib import asynccontextmanager

from fastapi import FastAPI


def fake_answer_to_everything_ml_model(x: float):
    return x * 42


ml_models = {}


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Load the ML model
    ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
    yield
    # Clean up the ML models and release the resources
    ml_models.clear()


app = FastAPI(lifespan=lifespan)


@app.get("/predict")
async def predict(x: float):
    result = ml_models["answer_to_everything"](x)
    return {"result": result}

๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ ๐Ÿ ๐Ÿ•ณ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ with ๐Ÿ“„, ๐Ÿ–ผ, open() ๐Ÿ’ช โš™๏ธ ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ:

with open("file.txt") as file:
    file.read()

โฎ๏ธ โฌ ๐Ÿ, ๐Ÿ“ค ๐Ÿ” ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ. ๐Ÿ‘† ๐Ÿ”œ โš™๏ธ โšซ๏ธ โฎ๏ธ async with:

async with lifespan(app):
    await do_stuff()

๐Ÿ•โ” ๐Ÿ‘† โœ ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ โš–๏ธ ๐Ÿ” ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ ๐Ÿ’– ๐Ÿ”›, โšซ๏ธโ” โšซ๏ธ ๐Ÿ”จ ๐Ÿ‘ˆ, โญ ๐Ÿ›ฌ with ๐Ÿซ, โšซ๏ธ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ“Ÿ โญ yield, & โฎ๏ธ โŽ with ๐Ÿซ, โšซ๏ธ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ“Ÿ โฎ๏ธ yield.

๐Ÿ‘† ๐Ÿ“Ÿ ๐Ÿ–ผ ๐Ÿ”›, ๐Ÿ‘ฅ ๐Ÿšซ โš™๏ธ โšซ๏ธ ๐Ÿ”—, โœ‹๏ธ ๐Ÿ‘ฅ ๐Ÿšถโ€โ™€๏ธ โšซ๏ธ FastAPI โšซ๏ธ โš™๏ธ โšซ๏ธ.

lifespan ๐Ÿ”ข FastAPI ๐Ÿ“ฑ โœŠ ๐Ÿ” ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐Ÿ‘† ๐Ÿ†• lifespan ๐Ÿ” ๐Ÿ”‘ ๐Ÿ‘จโ€๐Ÿ’ผ โšซ๏ธ.

from contextlib import asynccontextmanager

from fastapi import FastAPI


def fake_answer_to_everything_ml_model(x: float):
    return x * 42


ml_models = {}


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Load the ML model
    ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
    yield
    # Clean up the ML models and release the resources
    ml_models.clear()


app = FastAPI(lifespan=lifespan)


@app.get("/predict")
async def predict(x: float):
    result = ml_models["answer_to_everything"](x)
    return {"result": result}

๐ŸŽ› ๐ŸŽ‰ (๐Ÿ˜ข)

Warning

๐Ÿ‘ ๐ŸŒŒ ๐Ÿต ๐Ÿ•ด & ๐Ÿคซ โš™๏ธ lifespan ๐Ÿ”ข FastAPI ๐Ÿ“ฑ ๐Ÿ”ฌ ๐Ÿ”›.

๐Ÿ‘† ๐Ÿ’ช ๐ŸŽฒ ๐Ÿšถ ๐Ÿ‘‰ ๐Ÿ•.

๐Ÿ“ค ๐ŸŽ› ๐ŸŒŒ ๐Ÿ”ฌ ๐Ÿ‘‰ โš› ๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿ•ด & โฎ๏ธ ๐Ÿคซ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ (๐Ÿ”ข) ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ› ๏ธ โญ ๐Ÿˆธ โ–ถ๏ธ ๐Ÿ†™, โš–๏ธ ๐Ÿ•โ” ๐Ÿˆธ ๐Ÿคซ ๐Ÿ”ฝ.

๐Ÿ‘ซ ๐Ÿ”ข ๐Ÿ’ช ๐Ÿ“ฃ โฎ๏ธ async def โš–๏ธ ๐Ÿ˜ def.

startup ๐ŸŽ‰

๐Ÿšฎ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿƒ โญ ๐Ÿˆธ โ–ถ๏ธ, ๐Ÿ“ฃ โšซ๏ธ โฎ๏ธ ๐ŸŽ‰ "startup":

from fastapi import FastAPI

app = FastAPI()

items = {}


@app.on_event("startup")
async def startup_event():
    items["foo"] = {"name": "Fighters"}
    items["bar"] = {"name": "Tenders"}


@app.get("/items/{item_id}")
async def read_items(item_id: str):
    return items[item_id]

๐Ÿ‘‰ ๐Ÿ’ผ, startup ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ”ข ๐Ÿ”œ ๐Ÿ”ข ๐Ÿฌ "๐Ÿ’ฝ" ( dict) โฎ๏ธ ๐Ÿ’ฒ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐ŸŒ… ๐ŸŒ˜ 1๏ธโƒฃ ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ”ข.

& ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ† ๐Ÿšซ โ–ถ๏ธ ๐Ÿ“จ ๐Ÿ“จ โญ ๐ŸŒ startup ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ โœ”๏ธ ๐Ÿ.

shutdown ๐ŸŽ‰

๐Ÿšฎ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿƒ ๐Ÿ•โ” ๐Ÿˆธ ๐Ÿคซ ๐Ÿ”ฝ, ๐Ÿ“ฃ โšซ๏ธ โฎ๏ธ ๐ŸŽ‰ "shutdown":

from fastapi import FastAPI

app = FastAPI()


@app.on_event("shutdown")
def shutdown_event():
    with open("log.txt", mode="a") as log:
        log.write("Application shutdown")


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]

๐Ÿ“ฅ, shutdown ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ”ข ๐Ÿ”œ โœ โœ โธ "Application shutdown" ๐Ÿ“ log.txt.

Info

open() ๐Ÿ”ข, mode="a" โ›“ "๐ŸŽป",, โธ ๐Ÿ”œ ๐Ÿšฎ โฎ๏ธ โšซ๏ธโ” ๐Ÿ”› ๐Ÿ‘ˆ ๐Ÿ“, ๐Ÿต ๐Ÿ“ โฎ๏ธ ๐ŸŽš.

Tip

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘‰ ๐Ÿ’ผ ๐Ÿ‘ฅ โš™๏ธ ๐Ÿฉ ๐Ÿ open() ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ”— โฎ๏ธ ๐Ÿ“.

, โšซ๏ธ ๐Ÿ”Œ ๐Ÿ‘ค/๐Ÿ…พ (๐Ÿ”ข/๐Ÿ”ข), ๐Ÿ‘ˆ ๐Ÿšš "โŒ›" ๐Ÿ‘œ โœ ๐Ÿ’พ.

โœ‹๏ธ open() ๐Ÿšซ โš™๏ธ async & await.

, ๐Ÿ‘ฅ ๐Ÿ“ฃ ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ”ข โฎ๏ธ ๐Ÿฉ def โ†ฉ๏ธ async def.

Info

๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ… ๐Ÿ”ƒ ๐Ÿ‘ซ ๐ŸŽ‰ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ’ƒ ๐ŸŽ‰' ๐Ÿฉบ.

startup & shutdown ๐Ÿ‘ฏโ€โ™‚๏ธ

๐Ÿ“ค โ†• ๐Ÿคž ๐Ÿ‘ˆ โš› ๐Ÿ‘† ๐Ÿ•ด & ๐Ÿคซ ๐Ÿ”—, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š โ–ถ๏ธ ๐Ÿ•ณ & โคด๏ธ ๐Ÿ โšซ๏ธ, ๐Ÿ“Ž โ„น & โคด๏ธ ๐Ÿš€ โšซ๏ธ, โ™’๏ธ.

๐Ÿ”จ ๐Ÿ‘ˆ ๐Ÿ‘ฝ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿšซ ๐Ÿ’ฐ โš› โš–๏ธ ๐Ÿ”ข ๐Ÿ‘ฏโ€โ™‚๏ธ ๐ŸŒ… โš  ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ช ๐Ÿช ๐Ÿ’ฒ ๐ŸŒ ๐Ÿ”ข โš–๏ธ ๐ŸŽ ๐ŸŽฑ.

โ†ฉ๏ธ ๐Ÿ‘ˆ, โšซ๏ธ ๐Ÿ”œ ๐Ÿ‘ โ†ฉ๏ธ โš™๏ธ lifespan ๐Ÿ”ฌ ๐Ÿ”›.

๐Ÿ“ก โ„น

๐Ÿ“ก โ„น ๐Ÿ˜Ÿ ๐Ÿค“. ๐Ÿ‘ถ

๐Ÿ”˜, ๐Ÿ”ซ ๐Ÿ“ก ๐Ÿ”ง, ๐Ÿ‘‰ ๐Ÿ• ๐Ÿ”† ๐Ÿ› ๏ธ, & โšซ๏ธ ๐Ÿ”ฌ ๐ŸŽ‰ ๐Ÿค™ startup & shutdown.

๐ŸŽง ๐Ÿˆธ

๐Ÿ‘ถ โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐Ÿ‘ซ ๐Ÿ”† ๐ŸŽ‰ (๐Ÿ•ด & ๐Ÿคซ) ๐Ÿ”œ ๐Ÿ•ด ๐Ÿ› ๏ธ ๐Ÿ‘‘ ๐Ÿˆธ, ๐Ÿšซ ๐ŸŽง ๐Ÿˆธ - ๐Ÿ—ป.