【问题标题】:Google App Engine urlfetch DeadlineExceededError in push task handler running apiclient batch request运行 apiclient 批处理请求的推送任务处理程序中的 Google App Engine urlfetch DeadlineExceededError
【发布时间】:2016-10-04 16:32:42
【问题描述】:

我有一个任务处理程序正在向 Google 日历 API 发出批处理请求。 5 秒后,请求失败并显示 DeadlineExceededError:API 调用 urlfetch.Fetch() 响应时间过长并被取消。我已按照建议在我发出批处理请求的位置附近更改了 urlfetch.set_default_fetch_deadline(60) here 但似乎没有什么区别:截止日期似乎仍然是 5 秒。

我正在使用位于 oauth2client 和 httplib2 之上的 Python Google API 客户端库。但我的理解是,GAE 会拦截使用 urlfetch.Fetch 的底层调用。这也是堆栈跟踪似乎显示的内容。

您能看出urlfetch.set_default_fetch_deadline 似乎不起作用的任何原因吗?

编辑:

这是用于构建批处理请求的代码:

# note `http` is a oauth2client authorized http client
cal = apiclient.discovery.build('calendar','v3',http=http)
req = cal.new_batch_http_request(callback=_callback)
for event in events:   # anything larger than ~5 events in batch takes >5 secs
  req.add( 
    cal.events().patch(calendarId=calid, eventId=event["id"], body=self._value) 
  )
urlfetch.set_default_fetch_deadline(60)  # has no effect
req.execute()

【问题讨论】:

    标签: python google-app-engine google-api google-calendar-api task-queue


    【解决方案1】:

    所以,urlfetch.set_default_fetch_deadline() 最终确实为我工作。问题是我的底层 http 客户端(oauth2client / httplib2)本质上存储在全局中。一旦我在任务处理程序线程中创建了它,set_default_fetch_deadline 就起作用了。

    【讨论】:

      【解决方案2】:

      尝试添加deadline参数:

      my_result = urlfetch.fetch(my_url, deadline=15)

      【讨论】:

      • 谢谢,但我没有直接使用 urlfetch -- 而是 api client library。 AFAIK 没有直接的方法可以传递截止日期参数,特别是因为该库不使用 urlfetch,除非在 GAE 环境中。
      • 你能提供你用来发出请求的代码吗?你也许可以通过 kwargs,比如截止日期。
      • 完成。我注意到execute method 有一个num_retries kwarg,但这对我没有帮助。
      • 来自文档:“您可以使用 urlfetch.set_default_fetch_deadline() 函数调整默认期限。此函数将新的默认期限存储在线程局部变量中,因此必须为每个请求设置它,例如,在自定义中间件中。”
      • 我正在设置它,但它不会影响请求的截止日期 - 无论我是在创建请求对象之前还是之后设置它。我认为问题可能是底层的http客户端不是线程本地的。但是当我使它成为线程本地时,它会尝试刷新访问令牌并获得 AccessTokenRefreshError... 我将更新其中一些库,看看它是否有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多