Python 如何在硬超时时重试芹菜任务?

Python 如何在硬超时时重试芹菜任务?,python,celery,Python,Celery,我有一个芹菜任务如下: @celery.task(name='tasks.ht_run', default_retry_delay=30, max_retries=15, time_limit=1) def ht_run(str_command): try: f = os.popen(str_command) output = f.read() if output == '': raise Exception

我有一个芹菜任务如下:

@celery.task(name='tasks.ht_run', default_retry_delay=30, max_retries=15, time_limit=1)  
def ht_run(str_command):
    try:
        f = os.popen(str_command)
        output = f.read()
        if output == '':
            raise Exception
    except Exception as exc:
        raise ht_run.retry(exc=exc)

    return output.split('\n')
这就是所谓的: appserver.ht_run.delay(字符串)

而我希望它在超时时重试,而不是在失败时重试。在芹菜窗口中,我得到以下错误:

[2014-12-10 11:50:22,128: ERROR/MainProcess] Task tasks.ht_run[6a83793a-8bd6-47fc-bf74-0b673bf961f2] raised unexpected: TimeLimitExceeded(1,)
Traceback (most recent call last):
  File "/Users/andy.terhune/cgenv/lib/python2.6/site-packages/billiard/pool.py", line 639, in on_hard_timeout
    raise TimeLimitExceeded(job._timeout)
TimeLimitExceeded: TimeLimitExceeded(1,)
[2014-12-10 11:50:22,128: ERROR/MainProcess] Hard time limit (1s) exceeded for tasks.ht_run[6a83793a-8bd6-47fc-bf74-0b673bf961f2]
[2014-12-10 11:50:23,644: ERROR/MainProcess] Process 'Worker-28' pid:2081 exited with 'signal 9 (SIGKILL)'
我怎样才能让它超时并重试呢?

谢谢用户2097159

尝试了一次软暂停,效果很好

@celery.task(
    name='tasks.ht_run', 
    default_retry_delay=30,
    max_retries=15,
    soft_time_limit=1)
def ht_run(str_command):
    try:
        f = os.popen(str_command)
        output = f.read()
        if output == '':
            raise Exception
    except Exception as exc:
        raise ht_run.retry(exc=exc)

    return output.split('\n')


现在在4.0版中,您可以在decorator中指定特定的异常。以下是文档中的一个示例:

from twitter.exceptions import FailWhaleError

@app.task(autoretry_for=(FailWhaleError,))
def refresh_timeline(user):
    return twitter.refresh_timeline(user)

其中一个异常可以是
SoftTimeLimitExceed
,以满足您的情况。

有什么理由不使用软超时吗?我没有尝试过,只要它重试,我就可以。我只想指出,您不应该使用一般的
异常。软超时的正确例外情况是
softtimelimitexeceded
from twitter.exceptions import FailWhaleError

@app.task(autoretry_for=(FailWhaleError,))
def refresh_timeline(user):
    return twitter.refresh_timeline(user)