芹菜配置
芹菜
-
安裝 -
pip install django-celery
-
加
-
基本專案結構。
- src/ - bin/celery_worker_start # will be explained later on - logs/celery_worker.log - stack/__init __.py - stack/celery.py - stack/settings.py - stack/urls.py - manage.py
-
將
celery.py
檔案新增到stack/stack/
資料夾。from __future__ import absolute_import import os from celery import Celery os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stack.settings') from django.conf import settings # noqa app = Celery('stack') app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
-
到你的
stack/stack/__init__.py
新增以下程式碼:from __future__ import absolute_import from .celery import app as celery_app # noqa
-
建立一個任務並將其標記為例如
@shared_task()
@shared_task() def add(x, y): print("x*y={}".format(x*y))
-
手工執行芹菜工人:
celery -A stack worker -l info
如果你還想新增