用芹菜设置特定任务的时间限制

jim*_*ard 45 celery celeryd

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

mhe*_*her 61

您可以在定义任务或调用时设置任务时间限制(硬和/或软).

from celery.exceptions import SoftTimeLimitExceeded

@celery.task(time_limit=20)
def mytask():
    try:
        return do_work()
    except SoftTimeLimitExceeded:
        cleanup_in_a_hurry()
Run Code Online (Sandbox Code Playgroud)

要么

mytask.apply_async(args=[], kwargs={}, time_limit=30, soft_time_limit=10)
Run Code Online (Sandbox Code Playgroud)

  • 您还可以使用以下命令为配置中的任务设置时间限制:`CELERY_ANNOTATIONS = {'module.mytask':{'time_limit':20.0}}` (12认同)
  • 对于芹菜版本3.1.x,看起来`apply_async`的`timeout/soft_timeout`参数也被改为`time_limit/soft_time_limit`.这里介绍了相关的变化 - https://github.com/celery/celery/commit/be6cef2e441e5ecf5857aeb77bd885f06128b9c9 (7认同)

gog*_*sca 9

这是使用soft_time_limit = 10000 的特定Task和Celery 3.1.23的装饰器的示例

@task(bind=True, default_retry_delay=30, max_retries=3, soft_time_limit=10000)
def process_task(self, task_instance):
   """Task processing."""
        pass
Run Code Online (Sandbox Code Playgroud)

  • 你也可以像`@task(soft_time_limit=10, time_limit=15)`一样分配`time_limit` (2认同)