python – 在GAE中的deferred.defer中重试计数

我正在使用GAE的’
deffered‘库(python),它会在发生异常时自动重试任务.

有没有办法知道(在任务处理函数内)任务被尝试的次数?

我的最终目标是实现以下内容:

if num_tries >5:
  email_admins()
  raise deferred.PermanentTaskFailure

最初我以为我可以使用’TaskRetryOptions’来限制尝试次数,但我相信这并没有提供我的email_admins()调用的机制.或者是吗?

[编辑]当然我可以读取/写入DB或memcache的尝试次数,但我宁愿避免这种复杂性.如果可能的话,我更愿意从任务/任务队列中获取详细信息.

最佳答案 有几个标题将自动设置任务

https://developers.google.com/appengine/docs/python/taskqueue/overview-push

X-AppEngine-TaskRetryCount, the number of times this task has been
retried; for the first attempt, this value is 0. This number includes
attempts where the task failed due to a lack of available instances
and never reached the execution phase.

X-AppEngine-TaskExecutionCount, the number of times this task has previously failed during the execution phase. This number does not include failures due to a lack of available instances.

编辑1

这些值可以访问:

num_tries  = self.request.headers.get('X-AppEngine-TaskRetryCount')

编辑2

http://webapp-improved.appspot.com/api/webapp2.html#webapp2.get_request

对于deferred尝试:

request = webapp2.get_request()
点赞