我的任务进入芹菜并获得结果。我知道这是因为我能做到这一点。
>>> ts = TaskState.objects.all()[0]
>>> ts
Out[31]: <TaskState: SUCCESS apps.checklist.tasks.bulk_checklist_process(ec01461b-3431-478d-adfc-6d6cf162e9ad) ts:2012-07-20 14:35:41>
>>> ts.state
Out[32]: u'SUCCESS'
>>> ts.result
Out[33]: u'{\'info\': ["Great",]}'
但是当我尝试使用documented方式获得结果时 - 所有地狱都破裂了......
>>> from celery.result import BaseAsyncResult
>>> result = BaseAsyncResult(ts.task_id)
>>> result.get()
../lib/python2.7/site-packages/djcelery/managers.py:178: TxIsolationWarning: Polling results with transaction isolation level repeatable-read within the same transaction may give outdated results. Be sure to commit the transaction for each poll iteration.
"Polling results with transaction isolation level "
所以我有两个问题。
ts.state == SUCCESS
,我就可以使用ts.result
。这会带来什么样的缺点,导致什么样的格式?更新
所以第二部分很简单。 可怕但很容易..
context['results'] = resulting_values = result.get(propagate=False)
if not isinstance(resulting_values, dict):
context['results'] = resulting_values = eval(context['task'].result)
log.error("We should not be here..")
答案 0 :(得分:0)
它在你粘贴的警告中说得那么正确:
TxIsolationWarning: Polling results with transaction isolation level repeatable-read
within the same transaction may give outdated results. Be sure to commit the transaction
for each poll iteration.
将数据库用于结果,以及是否要在同一进程中轮询它们
那么您需要将数据库的隔离级别配置为READ-COMMITTED
或者在检查结果之前提交交易。
也弃用BaseAsyncResult
,请使用
from celery.result import AsyncResult