简体   繁体   中英

How do I retry Celery task until certain time?

I read the docs on Celery tasks and can't understand how I do what I need.

I want to start a task, run and retry it every 1 second. After 3 seconds it should stop retrying and return a default value.

Here's a POC code that doesn't work as expected:

@task(expires=3, default_retry_delay=1, max_retries=10)
def ttt(args):
    try:
        return slow_work_result(args)
    except SlowWorkFailed:
        pass

    try:
        return ttt.retry(countdown=1)
    except MaxRetriesExceededError:
        return False


ttt.apply_async(args=(1,)).get()

The task should expire in 3 seconds, but MaxRetriesExceededError should be raised in 10 seconds. But when I run it, it stops because of MaxRetriesExceededError .

What am I doing wrong?

Can this try-except construct be more elegant?

I wrote a task decorate to retry task maybe that can solve your problem.

https://gist.github.com/3958777

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM