apply_async(retry=False) 不会禁用任务级别的重试。如果您想禁用任务级别的重试,请在任务函数上设置 retry=False。
示例代码:
from celery import Celery
app = Celery('tasks', broker='pyamqp://localhost')
@app.task(bind=True, max_retries=3)
def add(self, x, y):
try:
result = x + y
return result
except Exception as e:
self.retry(exc=e, countdown=30, max_retries=3)
# 禁用任务级别的重试
@app.task(bind=True, max_retries=3, retry=False)
def subtract(self, x, y):
try:
result = x - y
return result
except Exception as e:
self.retry(exc=e, countdown=30, max_retries=3)
# 调用 task
res = add.apply_async(args=[1, 2], retry=True) # 允许任务级别的重试
res = subtract.apply_async(args=[2, 1], retry=False) # 禁用任务级别的重试