简体   繁体   English

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

[英]Why does celery return a KeyError when executing my task?

I keep getting this keyError.我不断收到这个keyError。 I am sending strings and id (integers) to the task function, so I don't think it is serialization issue.我正在向任务 function 发送字符串和 id(整数),所以我认为这不是序列化问题。 Also it says the keyerror is on the path to the function itself, not the contents.它还说密钥错误位于 function 本身的路径上,而不是内容。 Please help.请帮忙。

Tasks.py任务.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,
                       })

Relevant settings相关设置

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 output: Celery output:

[Tasks] [任务]

  . 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

In the celery output, you see that the task that gets picked up is在 celery 输出中,您会看到获取的任务是

tournaments.tasks.notify_match_creation (#1)

and in your key error it is在你的关键错误中

KeyError: 'apps.tournaments.tasks.notify_match_creation'

before you call the celery task, make sure that it is imported with the same name (structure) as it is in the celery task that gets picked up (#1).在调用 celery 任务之前,请确保它以与被拾取的 celery 任务中相同的名称(结构)导入(#1)。 Please refer to the link from celery docs for getting your relative imports rights.请参阅celery 文档中的链接以获取您的相关进口权。

one possible solution, when you launch celery - try一种可能的解决方案,当您启动芹菜时 - 尝试

celery worker -A apps.tournaments.tasks.notify_match_creation

this could align your task names这可以对齐您的任务名称

I had the exact same issue, however, the following worked for me,我有完全相同的问题,但是,以下对我有用,

in my tasks.py I changed from在我的tasks.py中,我从

from celery.task import task

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

to

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

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

I know it's late but this worked for me When I had the same error我知道已经晚了,但这对我有用当我遇到同样的错误时

I was using task.nameoftask我正在使用task.nameoftask

It is rather a relative import as它是一个相对的进口,因为

app.task.nameoftask app.task.nameoftask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM