[英]Celery Result error “args must be a list or tuple”
我正在運行一個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打印
您在任務中的代碼中收到錯誤,它在回溯中顯示:
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():
中for part in project.part_set.all():
迭代查詢集時,您已經獲得了當前部件實例for part in project.part_set.all():
這只是一個建議,你可能有更多的代碼需要這一步。
作為旁注,如果它是您項目中的任務而不是某些可重用應用程序的一部分,只需使用常規的@task
裝飾器,芹菜就會找到它,但請確保您正確配置Celery
應用程序,這是我的另一個可以指導您完成的帖子: Celery / Django Single Tasks被多次運行
因此,只要您正確配置了所有內容,就像以前一樣使用它:
@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,))
顯然我還建議直接通過調用沒有celery function(project.id)
測試任務,但我相信你知道的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.