Using Celery with Django

安装

pip install celery
pip install sqlalchemy

  • 可选安装
  1. 如果broker使用redis,则pip install -U "celery[redis]"

    此处有坑: celery4.0以上的broker只能用redis==2.10.6版本的,否则会报"AttributeError"

  2. 如果celery安装在windows上,需要额外安装eventlet. pip install eventlet

    此处又是坑: win10上运行celery4.x会报如下错误:
    ValueError: not enough values to unpack (expected 3, got 0)

    启动celery需要带上参数: celery -A worker -l info -P eventlet

  3. 安装flower, 可视化celery监控软件
    pip install flower
    启动flower: celery flower worker -A

配置Django

  • settings配置
from __future__ import absolute_import, unicode_literals
...
# Celery settings

CELERY_BROKER_URL = 'amqp://guest:guest@localhost//'
# CELERY_BROKER_URL = 'redis://localhost:6379/0'

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_BACKEND = 'db+sqlite:///results.sqlite'
CELERY_TASK_SERIALIZER = 'json'

其他参考: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-celery-with-django

你可能感兴趣的:(Using Celery with Django)