خطاف بدء التشغيل لدورة الحياة
افتحوا الاتصالات مرة واحدة عند بدء الخادم.
خطاف بدء التشغيل لدورة الحياة درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Boot Once, Use Many Times
Some setup should happen once when your server boots, like opening a database connection, not on every single tool call. 🚀
Meet the Lifespan
The lifespan is a hook that runs startup code before requests arrive, then runs teardown code after the server stops.
An Async Context Manager
A lifespan is an async context manager: code before yield is startup, and code after yield is shutdown.
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(server):
# startup
yield
# shutdownWire It into FastMCP
You hand your lifespan to the server with the lifespan argument, so FastMCP knows to run it around its life.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo", lifespan=lifespan)Open Expensive Things Here
The startup block is the right home for expensive setup: database pools, HTTP clients, or loading a model into memory.
Yield the Shared State
Whatever you yield becomes the shared context for every request, so build it once and pass it along.
async def lifespan(server):
db = await connect_db()
yield {"db": db}Startup Runs Before Any Tool
FastMCP guarantees your startup finishes before it accepts the first tool call, so resources are ready in time.
A Typed Context, Optionally
You can yield a small dataclass instead of a dict to get typed, autocompletable access to your shared objects.
from dataclasses import dataclass
@dataclass
class AppCtx:
db: objectKeep Startup Fast
Clients wait for startup, so keep it lean: connect what you need and defer slow optional work until it is first used.
One Lifespan Per Server
A server has exactly one lifespan. Group all your startup logic inside it rather than scattering setup across tools.
Handle Startup Failures
If startup raises an error, the server should refuse to start rather than run half-configured and fail mysteriously later.
Quick Check
Where in a lifespan does startup code go relative to yield?
Recap: The Lifespan Hook
You learned the lifespan: an async context manager that runs setup before yield, yields shared state, and tears down after. Next, share it. 🎯
الأسئلة الشائعة
هل درس «خطاف بدء التشغيل لدورة الحياة» مجاني؟
نعم — نص درس «خطاف بدء التشغيل لدورة الحياة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «خطاف بدء التشغيل لدورة الحياة»؟
افتحوا الاتصالات مرة واحدة عند بدء الخادم. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «خطاف بدء التشغيل لدورة الحياة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- خطاف بدء التشغيل لدورة الحياة
- مشاركة الحالة عبر سياق التطبيق
- الوصول إلى كائن السياق
- الإيقاف النظيف وتحرير الموارد