gpt4 book ai didi

python - 如何手动将 Celery 任务标记为已完成并设置其结果?

转载 作者:太空狗 更新时间:2023-10-29 22:15:53 24 4
gpt4 key购买 nike

我有这个 Celery 任务:

@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.resultNone,我想设置将数据作为结果传递给回调函数并将任务标记为已完成。

我尝试使用:

app.backend.mark_as_done(do_something.request.id, data)

它成功地设置了任务的状态和结果,但后来结果被设置为函数的返回值,这里是 None

最佳答案

我终于找到了存储任务状态和结果然后通过引发 Ignore 异常忽略任务的解决方案,例如:

from celery.exceptions import Ignore

@app.task
def do_something(with_this):

# store the state and result manually
# the SUCCESS state is set by this method
app.backend.mark_as_done(
do_something.request.id,
the_data_to_store
)

# we can also use update_state which calls
# backend.store_result just like mark_as_done
# but we have to set the state in this case
do_something.update_state(
state = celery.states.SUCCESS,
meta = the_data_to_store
)

# ignore the task so no other state is recorded
# like what was happening with my function in the question
# the task will still be acknowledged
raise Ignore()

当您无法返回要作为结果存储的数据时,这很有用。

关于python - 如何手动将 Celery 任务标记为已完成并设置其结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25826639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com