我正在运行一个Django网站并且刚刚让Celery运行,但我遇到了令人困惑的错误。以下是代码的结构。
在tests.py中:
from tasks import *
from celery.result import AsyncResult
project = Project.objects.create()
# initalize various sub-objects of the project
c = function.delay(project.id)
r = AsyncResult(c.id).ready()
f = AsyncResult(c.id).failed()
# wait until the task is done
while not r and not f:
r = AsyncResult(c.id).ready()
f = AsyncResult(c.id).failed()
self.assertEqual() #will fail because task fails
在tasks.py中:
from __future__ import absolute_import
from celery import shared_task
@shared_task
def function(project_id)
#a bunch of calculations followed by a save of the project
project = Project.objects.get(project=project_id)
for part in project.part_set.all():
partFunction(part.id)
p = Part.objects.get(id=part.id)
# add to various variables in project from variables in p
project.save()
在mainapp / settings.py中:
BROKER_URL = "amqp://ipaddress"
CELERY_RESULT_BACKEND='amqp'
CELERY_ACCEPT_CONTENT = ['json','pickle','msgpack','yaml']
CELERY_IGNORE_RESULT = False
celery调试控制台日志必须通过list / tuple:
[INFO/MainProcess] Received task: myapp.tasks.function[id]
[ERROR/MainProcess] Task myapp.tasks.function[id]
raised unexpected: ValueError('task args must be a list or tuple',)
Traceback:
File "/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/python2.7/site-packages/celery/app/trace.py", line 437, in __protected_call__
return self.run(*args, **kwargs)
File "/myapp/tasks.py", line 28, in function
p = Part.objects.get(id=part.id)
File "/python2.7/site-packages/celery/app/task.py", line 555, in apply_async
**dict(self._get_exec_options(), **options)
File "/python2.7/site-packages/celery/app/base.py", line 351, in send_task
reply_to=reply_to or self.oid, **options
File "celery/app/amqp.py", line 252, in publish_task
raise ValueError('task args must be a list or tuple')
ValueError: task args must be a list or tuple
我得到的错误如上,AsyncResult(c.id).result: task args must be a list or tuple
。这应该是一个简单的解决方案,但事实并非如此。当我把它变成这样的列表时:
inline = [project.id]
c = function.delay(inline)
然后改变主意并告诉我AsyncResult(c.id).result: int() argument must be a string or a number, not 'list'
你可以想象我对可能出现的问题感到非常困惑。
tasks.py
@shared_task
def function(app):
@app.task(name='myapp.function', bind=True)
def function(project_id):
tests.py
c = function.s(project.id).delay()
function.app print
答案 0 :(得分:6)
您在任务中的代码中出现错误,它显示在traceback中:
File "/myapp/tasks.py", line 28, in function
p = Part.objects.get(id=part.id)
你的代码似乎是对的,但从追溯看起来,芹菜有一个旧版本的任务被腌制。 非常重要,只要您更改task.py
内的任何内容,就会重新启动芹菜(可能即使您更改了其他文件,但我不这么认为)。这可能是你问题的原因,它让我陷入困境几次。
此外,没有理由单独从数据库中提取part
p = Part.objects.get(id=part.id)
,因为在迭代for part in project.part_set.all():
中的查询集时,您已经获得了当前的部分实例。这只是一个建议,你可能有更多的代码需要这一步。
作为旁注,如果它是项目中的任务而不是某些可重用应用程序的一部分,只需使用常规@task
装饰器,芹菜就会找到它,但请确保配置{{1} } app right,这是我的另一篇帖子,可以指导您完成:Celery / Django Single Tasks being run multiple times
因此,只要您正确配置了所有内容,就像以前一样使用它:
Celery
然后叫它:
@task #or @shared_task
def function(project_id)
#a bunch of calculations followed by a save of the project
project = Project.objects.get(project=project_id)
....
或:
result = function.delay(project.id)
显然我还建议在没有芹菜result = function.apply_async(args=(project.id,))
的情况下直接测试任务,但我相信你知道的。