🔆 🎉¶
👆 💪 🔬 ⚛ (📟) 👈 🔜 🛠️ ⏭ 🈸 ▶️ 🆙. 👉 ⛓ 👈 👉 📟 🔜 🛠️ 🕐, ⏭ 🈸 ▶️ 📨 📨.
🎏 🌌, 👆 💪 🔬 ⚛ (📟) 👈 🔜 🛠️ 🕐❔ 🈸 🤫 🔽. 👉 💼, 👉 📟 🔜 🛠️ 🕐, ⏮️ ✔️ 🍵 🎲 📚 📨.
↩️ 👉 📟 🛠️ ⏭ 🈸 ▶️ ✊ 📨, & ▶️️ ⏮️ ⚫️ 🏁 🚚 📨, ⚫️ 📔 🎂 🈸 🔆 (🔤 "🔆" 🔜 ⚠ 🥈 👶).
👉 💪 📶 ⚠ ⚒ 🆙 ℹ 👈 👆 💪 ⚙️ 🎂 📱, & 👈 💰 👪 📨, &/⚖️ 👈 👆 💪 🧹 🆙 ⏮️. 🖼, 💽 🔗 🎱, ⚖️ 🚚 🔗 🎰 🏫 🏷.
⚙️ 💼¶
➡️ ▶️ ⏮️ 🖼 ⚙️ 💼 & ⤴️ 👀 ❔ ❎ ⚫️ ⏮️ 👉.
➡️ 🌈 👈 👆 ✔️ 🎰 🏫 🏷 👈 👆 💚 ⚙️ 🍵 📨. 👶
🎏 🏷 🔗 👪 📨,, ⚫️ 🚫 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
.
🎧 🈸¶
👶 ✔️ 🤯 👈 👫 🔆 🎉 (🕴 & 🤫) 🔜 🕴 🛠️ 👑 🈸, 🚫 🎧 🈸 - 🗻.