Python+Celery:如何按调用忽略任务结果?
有没有办法在每次调用时忽略任务的结果?
比如说,我希望在处理网页请求时可以忽略任务的结果,但在我交互式运行任务时又想等待结果(可能包含调试信息等)。
我知道任务有一个叫 ignore_result
的标志,但我想具体了解一下,是否可以在每次调用时单独设置 ignore_result
(而不是全局设置)。
2 个回答
0
在使用 apply_async
或 delay
时,你可以设置 ignore_result=True/False
来决定是否忽略结果。
@app.task
def hello():
print('hello world')
# storing/rejecting results per invocation basis
res = hello.apply_async(ignore_result=True)
res1 = hello.apply_async(ignore_result=False)
如果你使用的是旧版本的 celery,可能会遇到 这个 错误。你可以在文档中详细了解如何使用 ignore_result
,具体内容可以在 这里 找到。
1
通常情况下,ignore_result 是一个任务的属性,只有工作者会用到它(用来决定是否要返回结果)。
不过,如果你使用自己的任务参数(别叫它 ignore_result),你可以让任务根据这个参数来设置它的 ignore_result 属性:
task mytask(please_ignore_result):
mytask.ignore_result = please_ignore_result