![](/img/trans.png)
[英]How to set a time limit to a task if not excute within a certain time then just remove it in celery
[英]Celery: Assigning specific time limit for certain Task
我有一个这样写的任务:
@async_runner.app.task(name='task_name')
def async_task():
async_runner.send_task(
task_fn=task_processing,
queue='queue_name',
options=async_runner.DEFAULT_RETRY_POLICY
)
我的默认任务时间限制是 30 分钟。 我想将此特定任务的时间限制增加到 1 小时。
如何为这一项任务设置不同的时间限制?
我已经看过这个,但我的问题是针对 Flask 的,以及 Celery 在 Flask 中的配置方式。 谢谢。
根据官方 Celery 文档,
“时间限制(–time-limit)是任务在执行它的进程被终止并被新进程替换之前可能运行的最大秒数。您还可以启用软时间限制(–soft-time-limit) ,这引发了一个异常,任务可以在硬时间限制杀死它之前捕捉到清理“
因此,例如,如果您希望添加软时间限制并在达到限制时捕获异常,您可以执行以下操作:
from celery.exceptions import SoftTimeLimitExceeded
@async_runner.app.task(name='task_name', soft_time_limit=600)
def async_task():
try:
async_runner.send_task(
task_fn=task_processing,
queue='queue_name',
options=async_runner.DEFAULT_RETRY_POLICY
)
except SoftTimeLimitExceeded as e:
DO SOMETHING
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.