为芹菜设置特定任务的时间限制 [英] Setting Time Limit on specific task with celery

查看:71
本文介绍了为芹菜设置特定任务的时间限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Celery中有一项任务,在正常运行时可能会运行10,000秒。但是,我所有其他任务都应在不到一秒钟的时间内完成。如何在不更改短期任务的时间限制的情况下为故意长期运行的任务设置时间限制?

I have a task in Celery that could potentially run for 10,000 seconds while operating normally. However all the rest of my tasks should be done in less than one second. How can I set a time limit for the intentionally long running task without changing the time limit on the short running tasks?

推荐答案

您可以设置任务时间限制(和/或<定义任务或调用时,使用href = http://docs.celeryproject.org/en/latest/userguide/tasks.html#Task.soft_time_limit rel = noreferrer>软件)。

You can set task time limits (hard and/or soft) either while defining a task or while calling.

from celery.exceptions import SoftTimeLimitExceeded

@celery.task(time_limit=20)
def mytask():
    try:
        return do_work()
    except SoftTimeLimitExceeded:
        cleanup_in_a_hurry()

mytask.apply_async(args=[], kwargs={}, time_limit=30, soft_time_limit=10)

这篇关于为芹菜设置特定任务的时间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆