代码基本配置正常,但是运行Celery -A myproject worker -l info运行的时候了一个AttributeError: ‘str’ object has no attribute 'iteritems’错误,找了两天看到一个博客里的文章,错误问题和我的问题内容差不多,但是类型不一样,说是版本问题,我的原redis版本是3.0.1的版本,然后就试了试,降低了一下我的redis版本为2.10.6的版本,谁知道就正常了
下面是我的代码
#celery中间人 redis://redis服务所在的ip地址:端口/数据库
BROKER_URL = 'redis://localhost:6379/0'
# celery结果返回,可用于跟踪结果
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
# celery内容等消息的格式设置
CELERY_ACCEPT_CONTENT = ['application/json', ]
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# celery时区设置,使用settings中TIME_ZONE同样的时区
CELERY_TIMEZONE = TIME_ZONE
# coding:utf-8
from __future__ import absolute_import, unicode_literals
# 引入celery实例对象
from .celery import app as celery_app
# coding:utf-8
from __future__ import absolute_import, unicode_literals
from celery import Celery
from django.conf import settings
import os
# 获取当前文件夹名,即为该Django的项目名
project_name = os.path.split(os.path.abspath('.'))[-1]
project_settings = '%s.settings' % project_name
# 设置环境变量
os.environ.setdefault('DJANGO_SETTINGS_MODULE', project_settings)
# 实例化Celery
app = Celery(project_name)
# 使用django的settings文件配置celery
app.config_from_object('django.conf:settings')
# Celery加载所有注册的应用
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
#coding:utf-8
from __future__ import unicode_literals
from django.db import models
class Blog(models.Model):
caption=models.CharField(max_length=30)
def __unicode__(self):
return self.caption
# coding:utf-8
from celery import task
import time
@task
def sendmail(email):
print('发送邮件到************************************************************** %s' % email)
time.sleep(5) # 休息5秒
print('success')
return True
#coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from .models import Blog
import json
from .tasks import sendmail
def tasks(request):
# # 耗时任务,发送邮件
sendmail.delay('[email protected]')
# 其他行为
data = list(Blog.objects.values('caption'))
#return HttpResponse("ok")
return HttpResponse(json.dumps(data), content_type='application/json')
from django.contrib import admin
from django.urls import path
from celery_demo import views
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^', views.tasks, name='tasks'),
]
1.迁移数据库
执行python manage.py makemirations
执行python manage.py migrate
2.在命令行执行
#myproject是我的工程名,如果不一样需要改动
Celery -A myproject worker -l info
如果出现以下回显,说明正常
D:\Pycharmproject\Pycharmproject\celerytest>Celery -A celerytest worker -l info
-------------- celery@Alex-PC v3.1.17 (Cipater)
---- **** -----
--- * *** * -- Windows-7-6.1.7601-SP1
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: celerytest:0x36cf630
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://localhost:6379/0
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery
[tasks]
. celerytest001.task.sendmail
[2018-06-27 14:19:54,128: INFO/MainProcess] Connected to redis://localhost:6379/
0
[2018-06-27 14:19:55,148: INFO/MainProcess] mingle: searching for neighbors
[2018-06-27 14:19:58,177: INFO/MainProcess] mingle: all alone
[2018-06-27 14:20:03,237: WARNING/MainProcess] c:\python27\lib\site-packages\cel
ery\fixups\django.py:254: UserWarning: Using settings.DEBUG leads to a memory le
ak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2018-06-27 14:20:03,242: WARNING/MainProcess] celery@Alex-PC ready.
下一步python manage.py runserver启动django
打开首页,页面会立即弹出,后台会收到一个发送邮件的任务
[2018-06-27 14:20:03,242: WARNING/MainProcess] celery@Alex-PC ready.
[2018-06-27 14:21:17,400: INFO/MainProcess] Received task: celerytest001.task.se
ndmail[38801961-f60a-482b-a63b-f5af003d1653]
[2018-06-27 14:21:17,413: WARNING/Worker-1] 发送邮件到************************************************************** [email protected]
[2018-06-27 14:21:22,418: WARNING/Worker-1] success