加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 11:30:56 所属栏目:Python 来源:网络整理
导读:我正在使用GAE的’ deffered‘库(python),它会在发生异常时自动重试任务. 有没有办法知道(在任务处理函数内)任务被尝试的次数? 我的最终目标是实现以下内容: if num_tries 5: email_admins() raise deferred.PermanentTaskFailure 最初我以为我可以使用’T
我正在使用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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读