当我点击网页上的按钮时,我编写了一个芹菜任务,在我的数据库中设置了一些值。一切都好。现在我想编写一个更复杂的任务(disambiguation_task),它将一个字符串返回给我的Django视图(1.6.5)。代码是:
task_id = disambiguation_task.apply_async([json.dumps(json_request)])
async_result = AsyncResult(id=task_id,app=disambiguation_task)
一旦我尝试获取结果(async_result.get()),它就会生成错误:
AsyncResult' object has no attribute 'replace'
带有以下追溯:
File "/home/patrick/django/entite-tracker-master/entitetracker/docentites/views.py" in get
466. result = async_result.get()
File "/usr/local/lib/python2.7/dist-packages/celery/result.py" in get
169. no_ack=no_ack,
File "/usr/local/lib/python2.7/dist-packages/celery/backends/amqp.py" in wait_for
155. on_interval=on_interval)
File "/usr/local/lib/python2.7/dist-packages/celery/backends/amqp.py" in consume
225. binding = self._create_binding(task_id)
File "/usr/local/lib/python2.7/dist-packages/celery/backends/amqp.py" in _create_binding
99. name = self.rkey(task_id)
File "/usr/local/lib/python2.7/dist-packages/celery/backends/amqp.py" in rkey
111. return task_id.replace('-', '')
Exception Type: AttributeError at /docentites/nodoc_desamb/news20150305NY501131/
Exception Value: 'AsyncResult' object has no attribute 'replace'
如果我尝试打印async_result.state,则会出现同样的错误。 有人可以帮我解决这个错误吗? 问候, 帕特里克
答案 0 :(得分:1)
disambiguation_task.apply_async([json.dumps(json_request)])
返回AsyncResult对象,而不是任务ID。简单地说:
task_result = disambiguation_task.apply_async([json.dumps(json_request)])
# if you need to use the task_id somewhere else
async_result = AsyncResult(id=task_result.id, app=disambiguation_task)