我想研究芹菜项目,但我不知道我在哪里弄错了。 'tasks.py'文件:
# -*- coding: utf-8 -*-
from celery import Celery
app = Celery('tasks', broker='sqla+sqlite:///celerydb.sqlite')
app.conf.update(
CELERY_RESULT_BACKEND = 'db+sqlite:///results.sqlite',
# CELERY_RESULT_BACKEND = 'redis://localhost/0',
# CELERY_RESULT_BACKEND = 'amqp',
# CELERY_RESULT_BACKEND = 'mongodb://127.0.0.1:27017/',
CELERY_TASK_SERIALIZER = 'json',
CELERY_IGNORE_RESULT = False,
)
@app.task(trail=True)
def add(x, y):
return x + y
@app.task(trail=True)
def mul(x, y):
return x * y
@app.task(trail=True)
def xsum(numbers):
return sum(numbers)
然后我像这样经营芹菜服务:
celery -A tasks worker --loglevel=info
效果很好。 然后我使用ipython来运行流脚本:
In [127]: result = add.delay(3,5)
In [128]: result.result
这样的错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-128-a030fb181312> in <module>()
----> 1 result.result
/home/xingming/pyvirt/local/lib/python2.7/site-packages/celery/result.pyc in result(self)
350 If the task raised an exception, this will be the exception
351 instance."""
--> 352 return self._get_task_meta()['result']
353 info = result
354
/home/xingming/pyvirt/local/lib/python2.7/site-packages/celery/result.pyc in _get_task_meta(self)
326 def _get_task_meta(self):
327 if self._cache is None:
--> 328 meta = self.backend.get_task_meta(self.id)
329 if meta:
330 state = meta['status']
/home/xingming/pyvirt/local/lib/python2.7/site-packages/celery/backends/base.pyc in get_task_meta(self, task_id, cache)
292 pass
293
--> 294 meta = self._get_task_meta_for(task_id)
295 if cache and meta.get('status') == states.SUCCESS:
296 self._cache[task_id] = meta
AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for'
但芹菜服务运作良好:
[2014-06-04 11:17:31,656: INFO/MainProcess] Received task: tasks.add[11baa65f-4e18-44b1-80e4-9e81626ca42a]
[2014-06-04 11:17:31,658: INFO/MainProcess] Task tasks.add[11baa65f-4e18-44b1-80e4-9e81626ca42a] succeeded in 0.00117350020446s: 8
这有什么问题?
感谢您的帮助!
答案 0 :(得分:1)
你不会犯错误。 Celery docs完全解释了这一点:默认情况下,未启用存储任务结果的后端,因此您无法使用任何使用它的函数。如果您想这样做,则需要按照文档配置后端。