Setting Time Limit on specific task with celery

后端 未结 2 1394
野趣味
野趣味 2020-12-01 05:10

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. Ho

相关标签:
2条回答
  • 2020-12-01 05:31

    This is an example with decorator for an specific Task and Celery 3.1.23 using soft_time_limit=10000

    @task(bind=True, default_retry_delay=30, max_retries=3, soft_time_limit=10000)
    def process_task(self, task_instance):
       """Task processing."""
            pass
    
    0 讨论(0)
  • 2020-12-01 05:38

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

    or

    mytask.apply_async(args=[], kwargs={}, time_limit=30, soft_time_limit=10)
    
    0 讨论(0)
提交回复
热议问题