0Pricing
Flask Academy · درس

تعريف مهمة واستدعاؤها

اكتب مهمة وأرسلها باستخدام delay.

تعريف مهمة واستدعاؤها درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

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

What a Task Is

A Celery task is just a Python function marked so workers can run it later. You decorate it once and it becomes enqueueable.

The task Decorator

Mark a function with the Celery task decorator. Now it has extra methods like delay that schedule it onto the queue.

@celery.task
def add(x, y):
    return x + y

Call It Normally vs Queued

Calling add(2, 3) runs it right now in your process. To run it on a worker instead, you call its delay method.

Enqueue with delay

The delay shortcut puts the job on the broker and returns immediately. The work happens later on a worker.

add.delay(2, 3)

apply_async for Options

Need more control like a countdown or queue name? Use apply_async, which takes args plus scheduling options.

add.apply_async(args=[2, 3], countdown=10)

You Get an AsyncResult

Both delay and apply_async return an AsyncResult. It carries the task id you use to look up the result later.

job = add.delay(2, 3)
print(job.id)

Arguments Must Serialize

Task arguments travel through the broker, so they must be serializable: use plain data like ints, strings, lists, and dicts.

Pass IDs, Not Objects

Do not send a database row to a task. Send its id instead, then re-fetch the object inside the task with a fresh session.

Enqueue Inside a View

A view validates input, enqueues the job, and returns a fast response. The user is not kept waiting for the slow part.

@app.post("/report")
def report():
    add.delay(2, 3)
    return {"status": "queued"}

Name Tasks Clearly

Give tasks descriptive names like send_welcome_email. Clear names make logs and the broker dashboard far easier to read. 🔍

Keep Tasks Small

A good task does one clear job. Small, focused tasks are easier to retry, monitor, and reason about when something breaks.

Quick Check

You want a function to run on a worker.

Recap

Decorate a function as a task, call delay or apply_async to queue it, pass serializable args, and enqueue from thin views. ✅

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

هل درس «تعريف مهمة واستدعاؤها» مجاني؟

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

ماذا ستتعلم في «تعريف مهمة واستدعاؤها»؟

اكتب مهمة وأرسلها باستخدام delay. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

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

كم من الوقت يستغرق درس «تعريف مهمة واستدعاؤها»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

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

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

  1. لماذا تنقل العمل خارج الطلب
  2. ربط Celery بمصنع التطبيق
  3. تعريف مهمة واستدعاؤها
  4. تتبّع النتائج ومعالجة حالات الفشل
← العودة إلى Flask Academy