ربط Celery بمصنع التطبيق
اضبط الوسيط وتطبيق Celery.
ربط Celery بمصنع التطبيق درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Three Pieces to Wire
To run Celery you need three things: the broker URL, a result backend, and a Celery app object your tasks belong to.
Pick a Broker
Redis is the easiest broker to start with. You point Celery at it with a URL so it knows where to push and pull jobs.
broker_url = "redis://localhost:6379/0"Add a Result Backend
If you want to read task results later, set a result backend. Redis works fine for both broker and backend in development.
result_backend = "redis://localhost:6379/1"The Celery App Object
Celery centers on one Celery() instance. Tasks are registered on it, and workers load it to know what to run.
from celery import Celery
celery = Celery(__name__)Why the Factory Matters
In the app factory pattern, config is loaded inside create_app. Celery should pull its broker URL from that same config, not hardcode it.
Read Config from Flask
Store CELERY settings in your Flask config so dev, test, and prod can differ. Then build Celery from app.config inside the factory.
app.config["CELERY_BROKER_URL"] = "redis://localhost:6379/0"A make_celery Helper
A common pattern is a make_celery(app) function that reads app.config and returns a configured Celery instance.
def make_celery(app):
c = Celery(app.import_name)
c.conf.update(app.config)
return cTasks Need App Context
Tasks often touch the database or current_app, which need an app context. Without it, those calls fail when the worker runs.
Subclass the Task Base
The classic fix wraps each task so it pushes an app context before running. That way tasks can use Flask extensions safely.
Two Processes, One Config
The web process and the worker are separate, but both build Celery from the same config so they agree on the broker and backend.
Start a Worker
You run workers from the command line, pointing at your Celery instance. Then they listen for jobs on the broker.
celery -A app.celery worker --loglevel=infoQuick Check
Tasks frequently use the database.
Recap
Set a broker and backend, build a Celery from app.config inside create_app, and push an app context so tasks can use Flask. ✅
الأسئلة الشائعة
هل درس «ربط Celery بمصنع التطبيق» مجاني؟
نعم — نص درس «ربط Celery بمصنع التطبيق» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «ربط Celery بمصنع التطبيق»؟
اضبط الوسيط وتطبيق Celery. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «ربط Celery بمصنع التطبيق»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا تنقل العمل خارج الطلب
- ربط Celery بمصنع التطبيق
- تعريف مهمة واستدعاؤها
- تتبّع النتائج ومعالجة حالات الفشل