Looking for django Keywords? Try Ask4Keywords

Django 芹菜配置


芹菜

  1. 安装 - pip install django-celery

  2. 基本项目结构。

     - 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
    
  3. 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)
    
  4. 到您的stack/stack/__init__.py添加以下代码:

     from __future__ import absolute_import
     from .celery import app as celery_app  # noqa
    
  5. 创建一个任务并将其标记为例如@shared_task()

     @shared_task()
     def add(x, y):
         print("x*y={}".format(x*y))
    
  6. “手工”运行芹菜工人:

    celery -A stack worker -l info如果你还想添加