django 为什么celery在执行任务时返回KeyError?

jum4pzuy  于 2023-05-19  发布在  Go
关注(0)|答案(4)|浏览(358)

我一直得到这个keyError。我发送字符串和id(整数)到任务函数,所以我不认为这是序列化问题。它还说keyerror是在函数本身的路径上,而不是内容上。请帮帮我

Tasks.py:

from celery.decorators import task
from notification import models as notification

@task(ignore_result=True)
def notify_match_creation(match, home_team, away_team, home_team_captain, away_team_captain):
    notification.send(User.objects.filter(profile__teams__pk__in=(home_team, away_team)),
                      "tournaments_new_match",
                      {'match': unicode(match),
                       'home_team_captain': home_team_captain,
                       'away_team_captain': away_team_captain,
                       })

相关设置:

CELERY_RESULT_BACKEND = "database"
CELERY_RESULT_DBURI = "postgresql://user:pass@localhost/ahgl"
BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"

celery 产量:

【任务】

. apps.tournaments.tasks.notify_match_creation
  . tournaments.tasks.notify_match_creation
[2012-02-25 02:34:06,209: WARNING/MainProcess] celery@NATTOWER has started.
[2012-02-25 02:34:06,477: WARNING/PoolWorker-4] E:\Webdesign\ahgl\ENV\lib\site-packages\djcelery\loaders.py:84: UserWarn
ing: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn("Using settings.DEBUG leads to a memory leak, never "
[2012-02-25 02:34:06,479: WARNING/PoolWorker-2] E:\Webdesign\ahgl\ENV\lib\site-packages\djcelery\loaders.py:84: UserWarn
ing: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn("Using settings.DEBUG leads to a memory leak, never "
[2012-02-25 02:34:06,523: WARNING/PoolWorker-3] E:\Webdesign\ahgl\ENV\lib\site-packages\djcelery\loaders.py:84: UserWarn
ing: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn("Using settings.DEBUG leads to a memory leak, never "
[2012-02-25 02:34:06,566: WARNING/PoolWorker-1] E:\Webdesign\ahgl\ENV\lib\site-packages\djcelery\loaders.py:84: UserWarn
ing: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn("Using settings.DEBUG leads to a memory leak, never "
[2012-02-25 02:34:31,520: INFO/MainProcess] Got task from broker: apps.tournaments.tasks.notify_match_creation[4dbd6258-
5cee-49e9-8c8a-2d2105a2d52a]
[2012-02-25 02:34:31,569: ERROR/MainProcess] Task apps.tournaments.tasks.notify_match_creation[4dbd6258-5cee-49e9-8c8a-2
d2105a2d52a] raised exception: KeyError('apps.tournaments.tasks.notify_match_creation',)
Traceback (most recent call last):
  File "E:\Webdesign\ahgl\ENV\lib\site-packages\celery\concurrency\processes\pool.py", line 211, in worker
    result = (True, func(*args, **kwds))
  File "E:\Webdesign\ahgl\ENV\lib\site-packages\celery\worker\job.py", line 50, in execute_and_trace
    task = tasks[name]
KeyError: 'apps.tournaments.tasks.notify_match_creation'
[2012-02-25 02:38:29,773: WARNING/MainProcess] celeryd: Hitting Ctrl+C again will terminate all running tasks!
[2012-02-25 02:38:29,773: WARNING/MainProcess] celeryd: Warm shutdown (MainProcess)
[2012-02-25 02:38:31,779: INFO/MainProcess] process shutting down
rta7y2nd

rta7y2nd1#

在celery输出中,您可以看到所拾取的任务是

tournaments.tasks.notify_match_creation (#1)

在你的键盘错误中

KeyError: 'apps.tournaments.tasks.notify_match_creation'

在调用celery任务之前,请确保导入的celery任务的名称(结构)与所选取的celery任务(#1)中的名称(结构)相同。请参考celery 文档的链接以获得您的相对进口权。
一个可能的解决方案,当你启动celery -尝试

celery worker -A apps.tournaments.tasks.notify_match_creation

这可以对齐您的任务名称

dy1byipe

dy1byipe2#

我也遇到了同样的问题,但是,以下方法对我有效,
在我的www.example.com中tasks.py我从

from celery.task import task

@task(name="generate_and_email_membership_invoice")
def my_task_name():
      ...

from ..celeryconf import app # the file where you have app.autodiscover_tasks()

@app.task()
def my_task_name():
     ..
4dc9hkyq

4dc9hkyq3#

它不是使用task.nameoftask,而是一个相对导入,如下所示

app.task.nameoftask
xsuvu9jc

xsuvu9jc4#

我得到了类似的KeyError下面celery 工人:

Traceback (most recent call last):
  File "C:\Users\kai\AppData\Local\Programs\Python\Python39\lib\site-packages\celery\worker\consumer\consumer.py", line 591, in on_task_received
    strategy = strategies[type_]
KeyError: 'account.tasks.display'

因为我在store/tasks.py中有display任务,如下所示:

# "store/tasks.py"

from celery import shared_task

@shared_task
def display(arg):
    return arg

然后,我使用错误的路径"account.tasks.display"store/tasks.py中使用display任务,如下所示:

# "core/settings.py"

CELERY_BEAT_SCHEDULE = {
    "scheduled_task": {
        "task": "account.tasks.display", # Here
        "schedule": 5.0,
        "args": ["Test"],
    }
}

所以为了解决这个错误,我使用了正确的路径"store.tasks.display",如下所示,然后错误就解决了:

# "core/settings.py"

CELERY_BEAT_SCHEDULE = {
    "scheduled_task": {
        "task": "store.tasks.display", # Here
        "schedule": 5.0,
        "args": ["Test"],
    }
}

相关问题