設定celery retry隨次數越時間越長 - jenhaoyang/backend_blog GitHub Wiki

@celery_app.task(max_retries=10)
def notify_gcm_device(device_token, message, data=None):
  notification_json = build_gcm_json(message, data=data)
 
  try:
    gcm.notify_device(device_token, json=notification_json)  except ServiceTemporarilyDownError:
    # Find the number of attempts so far
    num_retries = notify_gcm_device.request.retries
    seconds_to_wait = 2.0 ** num_retries    # First countdown will be 1.0, then 2.0, 4.0, etc.
    raise notify_gcm_device.retry(countdown=seconds_to_wait)

參考:
https://medium.com/@taylorhughes/three-quick-tips-from-two-years-with-celery-c05ff9d7f9eb