【问题标题】:Celery task not working in Django framework芹菜任务在 Django 框架中不起作用
【发布时间】:2019-10-05 21:45:27
【问题描述】:

我尝试使用 Django 框架中的 Celery 和 Redis Broker 将代码作为异步任务发送给用户 5 次。我的 Celery 服务器正在工作,它正在响应 celery cli 界面,即使它正在接收来自 Django 的任务,但之后我收到了类似的错误

Traceback (most recent call last):
  File "c:\users\vipin\appdata\local\programs\python\python3
es\billiard\pool.py", line 358, in workloop
    result = (True, prepare_result(fun(*args, **kwargs)))
  File "c:\users\vipin\appdata\local\programs\python\python3
es\celery\app\trace.py", line 544, in _fast_trace_task
    tasks, accept, hostname = _loc
ValueError: not enough values to unpack (expected 3, got 0) 



task.py -
from celery.decorators import task
from django.core.mail import EmailMessage
import time

@task(name="Sending_Emails")
def send_email(to_email,message):
    time1 = 1
    while(time1 != 5):
        print("Sending Email")
        email = EmailMessage('Checking Asynchronous Task', message+str(time1), to=[to_email])
        email.send()
        time.sleep(1)
        time1 += 1

views.py - 
print("sending for Queue")
send_email.delay(request.user.email,"Email sent : ")
print("sent for Queue")

settings.py - 
# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/India'


celery.py - 
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ECartApplication.settings')
app = Celery('ECartApplication')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

我希望电子邮件应该发送 5 次但收到错误

[tasks]
  . ECartApplication.celery.debug_task
  . Sending_Emails

[2019-05-19 12:41:27,695: INFO/SpawnPoolWorker-2] child process 3628 calling sel
f.run()
[2019-05-19 12:41:27,696: INFO/SpawnPoolWorker-1] child process 5748 calling sel
f.run()
[2019-05-19 12:41:28,560: INFO/MainProcess] Connected to redis://localhost:6379/
/
[2019-05-19 12:41:30,599: INFO/MainProcess] mingle: searching for neighbors
[2019-05-19 12:41:35,035: INFO/MainProcess] mingle: all alone
[2019-05-19 12:41:39,069: WARNING/MainProcess] c:\users\vipin\appdata\local\prog
rams\python\python37-32\lib\site-packages\celery\fixups\django.py:202: UserWarni
ng: Using settings.DEBUG leads to a memory leak, never use this setting in produ
ction environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2019-05-19 12:41:39,070: INFO/MainProcess] celery@vipin-PC ready.
[2019-05-19 12:41:46,448: INFO/MainProcess] Received task: Sending_Emails[db10da
d4-a8ec-4ad2-98a6-60e8c3183dd1]
[2019-05-19 12:41:47,455: ERROR/MainProcess] Task handler raised error: ValueErr
or('not enough values to unpack (expected 3, got 0)')
Traceback (most recent call last):
  File "c:\users\vipin\appdata\local\programs\python\python37-32\lib\site-packag
es\billiard\pool.py", line 358, in workloop
    result = (True, prepare_result(fun(*args, **kwargs)))
  File "c:\users\vipin\appdata\local\programs\python\python37-32\lib\site-packag
es\celery\app\trace.py", line 544, in _fast_trace_task
    tasks, accept, hostname = _loc
ValueError: not enough values to unpack (expected 3, got 0)

【问题讨论】:

    标签: django redis celery


    【解决方案1】:

    这是在 Windows 7/10 上运行 Python 时出现的问题。

    有一个解决方法,您只需要使用可以使用pip 安装的模块eventlet

    pip 安装小事件

    然后在命令末尾使用-P eventlet 执行您的工作人员:

    celery -A MyWorker worker -l info -P eventlet

    【讨论】:

    • 这是您使用 Celery 4.x 时的错误。就我而言:4.3.0(大黄)
    • 您有相关问题的链接吗?我想进一步挖掘。
    猜你喜欢
    • 2016-04-24
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2019-01-29
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多