Django + Celery:' str'对象没有属性" publish_task'

时间:2016-11-01 21:32:03

标签: django celery

我使用Celery与Django异步发送电子邮件。当我尝试使用某项任务时,收到错误消息'str' object has no attribute 'publish_task'。从日志中可以看出,错误在于Celery的代码,但我的代码也可能是源代码。这是我的日志:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/reminder/

Django Version: 1.8.5
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'crispy_forms',
 'bootstrap3_datetime',
 'haystack',
 'registration',
 'tinymce',
 'djcelery',
 'djcelery_email',
 'RemindApp',
 'home',
 'BookRoom',
 'GameDev')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.middleware.locale.LocaleMiddleware')


Traceback:
File "C:\Users\Tony\Desktop_Files\Envs\ReminderVenv\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Tony\Desktop_Files\Envs\ReminderVenv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "C:\Users\Tony\Desktop_Files\Programming\Django\Reminder\RemindApp\views.py" in index
  49.                                     request.user.email)
File "C:\Users\Tony\Desktop_Files\Envs\ReminderVenv\lib\site-packages\celery\app\task.py" in apply_async
  565.             **dict(self._get_exec_options(), **options)
File "C:\Users\Tony\Desktop_Files\Envs\ReminderVenv\lib\site-packages\celery\app\base.py" in send_task
  350.             task_id = P.publish_task(

Exception Type: AttributeError at /reminder/
Exception Value: 'str' object has no attribute 'publish_task'

另外,这是我的任务:

@celery.task
def Mailer_Send(reminder, reminder_title, user, email):
    mail = Mailer()
    mail.send_messages(
        subject = "Time's up! {} is due".format(reminder_title),
        template = 'reminder/email.html',
        context = {'user': user, 'post': reminder},
        to_emails = [email]
    )
    logger.info('Sending email for reminder: {}'.format(reminder_title))

以下是我尝试使用该任务的地方:

Mailer_Send.apply_async(reminder_note,
                        reminder_note.title,
                        request.user,
                        request.user.email)

2 个答案:

答案 0 :(得分:1)

你不能这样称呼它。

您需要像这样调用apply_async:

Mailer_Send.apply_async(args=[reminder_note,
                    reminder_note.title,
                    request.user,
                    request.user.email])

Mailer_Send.delay(reminder_note,
                    reminder_note.title,
                    request.user,
                    request.user.email)

查看文档:    Calling Celery Tasks

答案 1 :(得分:0)

您还可以添加倒计时变量,该变量将在您运行异步任务之前倒计时。