0Pricing
FastAPI Backend Development Bootcamp · درس

المهام الخلفية وطوابير الوظائف

تعلّم تفريغ الأعمال البطيئة من معالجات الطلبات باستخدام مهام FastAPI الخلفية وطوابير الوظائف الموزعة مثل Celery.

المهام الخلفية وطوابير الوظائف درس مجاني في FastAPI Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في FastAPI Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة FastAPI Backend Development Bootcamp 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Offload Work?

Some work, like sending email, generating reports, or processing images, is too slow to do inside a request.

Making the user wait hurts latency. Instead, respond fast and do the heavy work in the background.

FastAPI BackgroundTasks

FastAPI ships a lightweight BackgroundTasks tool for work that should run after the response is sent.

from fastapi import BackgroundTasks

def write_log(msg: str):
    with open("log.txt", "a") as f:
        f.write(msg)

@app.post("/signup")
def signup(bg: BackgroundTasks):
    bg.add_task(write_log, "new user")
    return {"ok": True}

Limits of BackgroundTasks

BackgroundTasks runs in the same process. If the server restarts, the work is lost, and it cannot scale across machines.

For durable, distributed work you need a real job queue.

What is a Job Queue?

A job queue stores tasks in a broker such as Redis or RabbitMQ. Separate worker processes pull tasks and execute them, independent of the web server.

Defining a Celery Task

Celery is the most common Python job queue. You decorate a function as a task.

from celery import Celery

celery_app = Celery("tasks", broker="redis://localhost:6379/0")

@celery_app.task
def send_email(to: str):
    # slow work here
    return "sent to " + to

Enqueuing from FastAPI

Trigger the task with .delay(); it returns immediately while a worker runs it later.

@app.post("/notify")
def notify(email: str):
    send_email.delay(email)
    return {"queued": True}

Running Workers

Workers are separate processes you scale independently of the web tier.

celery -A tasks worker --loglevel=info --concurrency=4

Tracking Task Results

With a result backend, you can store and query task status and outcomes.

result = send_email.delay("a@b.com")
print(result.id, result.status)

Retries and Reliability

Job queues can automatically retry failed tasks with backoff, something in-process tasks cannot do.

@celery_app.task(bind=True, max_retries=3)
def process(self, item):
    try:
        do_work(item)
    except Exception as exc:
        raise self.retry(exc=exc, countdown=10)

Scheduling Periodic Jobs

Celery Beat runs tasks on a schedule, like nightly cleanups or hourly syncs.

celery_app.conf.beat_schedule = {
  "cleanup": {"task": "tasks.cleanup", "schedule": 3600.0}
}

Idempotent Tasks

Because a queued task may be retried, design tasks to be idempotent so running them twice causes no harm, for example by checking if the email was already sent.

Quick Check

Test your background work knowledge.

Recap

You learned to keep request handlers fast:

  • BackgroundTasks handles simple in-process after-response work
  • Job queues like Celery give durable, distributed, retryable tasks
  • Scale workers independently and schedule periodic jobs

Offloading slow work keeps your API responsive under load.

الأسئلة الشائعة

هل درس «المهام الخلفية وطوابير الوظائف» مجاني؟

نعم — نص درس «المهام الخلفية وطوابير الوظائف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة FastAPI Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة FastAPI Backend Development Bootcamp 4 دروس في المجموع.

ماذا ستتعلم في «المهام الخلفية وطوابير الوظائف»؟

تعلّم تفريغ الأعمال البطيئة من معالجات الطلبات باستخدام مهام FastAPI الخلفية وطوابير الوظائف الموزعة مثل Celery. تتمرن على FastAPI Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ FastAPI Backend Development Bootcamp؟

لا تُشترط خبرة سابقة. FastAPI Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «المهام الخلفية وطوابير الوظائف»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس FastAPI Backend Development Bootcamp هذا؟

نعم. كل درس في FastAPI Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استراتيجيات التخزين المؤقت باستخدام Redis
  2. الوصول غير المتزامن إلى قاعدة البيانات
  3. موازنة التحميل والمراقبة
  4. المهام الخلفية وطوابير الوظائف
← العودة إلى FastAPI Backend Development Bootcamp