【发布时间】:2014-11-07 17:11:07
【问题描述】:
我有这个芹菜任务:
@app.task
def do_something(with_this):
# instantiate a class from a third party library
instance = SomeClass()
# this class uses callbacks to send progress info about
# the status and progress of what we're doing
def progress_callback(data):
# this status will change to 'finished' later
# but the return value that I want as the task result won't be returned
# so this is where I should mark the task as done manually
if data['status'] == 'working':
# I create a custom state for this task
do_something.update_state(
state = 'PROGRESS',
meta = data['progress']
)
# adding the callback to the instance
instance.add_callback(progress_callback)
# use the instance to do what I want
# this functions returns a value that I don't want as the task result
# so leaving this function without a return statement will make it None
instance.do_job(with_this)
如何将任务标记为手动完成?
在这种情况下,函数在没有任何return 语句的情况下结束,所以我得到的task.result 是None,我想将传递给回调函数的数据设置为结果并将任务标记为完成。
我尝试使用:
app.backend.mark_as_done(do_something.request.id, data)
它成功设置了任务的状态和结果,但后来结果被设置为函数的返回值,这里是None。
【问题讨论】: