[英]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.