celery==3.1.26
Django==1.11.21
django-celery==3.3.0
flower==0.9.3
kombu==3.0.37
redis==2.10.6
创建项目
django-admin.py startproject celery_test
创建app
python manange.py startapp kecelety
在app中创建tasks.py
celery_test]# cat kecelety/tasks.py
#!/usr/bin/env python
#coding:utf-8
import django
django.setup()
from celery import task, platforms
import time
# 允许root 用户运行celery
platforms.C_FORCE_ROOT = True
@task
def say_hello():
#主要是为了测试异步功能,让他休眠5秒再打印,这样就避免打印和views.py中的返回同时执行,
#其实是异步执行,为了肉眼效果就加上休眠
time.sleep(5)
print "say_hello()..."
return 0
@task
def add(x,y):
print(x,y)
return x+y
@task
def test(arg1, *arg2, **arg3):
print(arg1, arg2, arg3)
return 0
添加异步队列
celery_test]# cat kecelety/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from .tasks import say_hello
import json
def hello(request):
#添加到消息队列中
r = say_hello.delay()
#执行成功返回True
print r.ready()
print r.successful()
return HttpResponse(json.dumps({'data':'ddd'}))
urls.py
from kecelety.views import *
urlpatterns = [
...
url(r'^c/hello/', hello),
]
setting.py配置
INSTALLED_APPS = [
...
'djcelery',
'kecelety',
'kombu.transport.django', #配合下面的BROKER_URL使用
]
最后添加
import djcelery
djcelery.setup_loader()
CELERY_TIMEZONE = 'Asia/Shanghai'
#息队列界面UI中可以看到
BROKER_URL = 'redis://127.0.0.1:6379/0' #设置redis为celery代理
#设置django为celery代理,此时任务消息存储在数据库中,在admin中注册即可在后台看到
#admin.py:
#from kombu.transport.django import models as kombu_models
#admin.site.register(kombu_models)
#BROKER_URL = 'django://'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' #定时任务用到
CELERYD_MAX_TASKS_PER_CHILD = 3 # 每个worker最多执行3个任务就会被销毁,可防止内存泄露
开启服务
python manage.py celery worker -c 4 --loglevel=info
python manage.py runserver 0.0.0.0:9004
python manage.py celery flower
python manage.py celery beat
可以在admin上添加定时任务,也可以从网
已知错误
django celery运行报错 TypeError: can only concatenate list (not "tuple") to list
\lib\site-packages\djcelery\management\commands\celery.py
14 # options = (CeleryCommand.options +
15 # base.get_options() +
16 # base.preload_options)
https://www.cnblogs.com/cpl9412290130/p/10597291.html
celery:Unrecoverable error: AttributeError("'unicode' object has no attribute 'iteritems')
pip uninstall redis
pip install redis==2.10.6
阅读(9285) | 评论(0) | 转发(0) |