Python+Celery:忽略每次调用的任务结果?

发布于 2024-09-27 20:39:56 字数 199 浏览 2 评论 0原文

是否可以在每次调用的基础上忽略任务结果?

例如,这样我可以在 Web 请求期间运行任务时忽略任务的结果,但在交互运行任务时等待结果(可能有调试信息)?

我知道任务具有 ignore_result 标志,但我特别想知道是否可以在每次调用的基础上(而不是“全局”基础)设置 ignore_result

Is it possible to ignore task results on a per-invocation basis?

For example, so I can ignore the results of tasks when they are being run during a web request, but wait for the result (which might have, eg, debug info) when I'm running the task interactively?

I know that Tasks have the ignore_result flag, but I'm wondering specifically if it's possible to set ignore_result on a per-invocation basis (not a "global" basis).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长发绾君心 2024-10-04 20:39:56

通常不会,因为ignore_result是只有工作人员使用的任务的属性(以决定是否发回结果)。

但是,如果您使用自己的任务参数(避免将其称为ignore_result),并让任务根据该参数设置其ignore_result,则可以做到这一点:

task mytask(please_ignore_result):
    mytask.ignore_result = please_ignore_result

Not normally, because ignore_result is a property of a Task that only the workers use (to decide whether to send a result back).

But you could do it if you used your own task parameter (avoid calling it ignore_result), and have the task set its ignore_result based on that:

task mytask(please_ignore_result):
    mytask.ignore_result = please_ignore_result
绅刃 2024-10-04 20:39:56

您可以在调用 apply_asyncdelay 时使用 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 的文档 此处

You can use ignore_result=True/False while calling apply_async or delay

@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)

You might run into this error if you are running an older version of celery. You can read the docs about how to use ignore_result in more detail here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文