python 如何在 Google App Engine 中为 urlfetch 设置超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2162115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:56:45  来源:igfitidea点击:

How to set timeout for urlfetch in Google App Engine?

pythondjangogoogle-app-enginetimeout

提问by GRex

I'm trying to have Django (on top of GAE) fetch data from another web service. I'm often hit with error like this:

我正在尝试让 Django(在 GAE 之上)从另一个 Web 服务获取数据。我经常遇到这样的错误:

ApplicationError: 2 timed out Request

Method: GET

Request URL:http://localhost:8080/

Exception Type: DownloadError

Exception Value: ApplicationError: 2 timed out

Exception Location: /google_appengine/google/appengine/api/urlfetch.py in _get_fetch_result, line 325

应用程序错误:2 超时请求

方法:获取

请求地址:http://localhost:8080/

异常类型:下载错误

异常值:ApplicationError:2 超时

异常位置:/google_appengine/google/appengine/api/urlfetch.py​​ in _get_fetch_result, line 325

It feels as if it will time out only after 12 seconds (I'm not sure, but it's really short).

感觉好像它只会在 12 秒后超时(我不确定,但它真的很短)。

Question: how can I set a longer timeout?

问题:如何设置更长的超时时间?

回答by Alex Young

Seeing as this is a Pythonquestion, I thought I'd provide a Python answer for anyone who comes across this problem.

看到这是一个Python问题,我想我会为遇到此问题的任何人提供 Python 答案。

Just import urlfetchand then define a deadline before doing anything else in your code:

只需导入urlfetch然后在代码中执行任何其他操作之前定义一个截止日期:

from google.appengine.api import urlfetch

urlfetch.set_default_fetch_deadline(60)

回答by Mark Bell

You can set it using the deadlineargument of the fetch function. From the docs:

您可以使用fetch 函数deadline参数来设置它。从文档

The deadline can be up to a maximum of 60 seconds for request handlers and 10 minutes for tasks queue and cron job handlers. If deadline is None, the deadline is set to 5 seconds.

请求处理程序的截止时间最长为 60 秒,任务队列和 cron 作业处理程序的截止时间最长为 10 分钟。如果截止时间为无,则截止时间设置为 5 秒。



Edit:looks like this has changed now. From here:

编辑:看起来现在已经改变了。从这里

You can set a deadline for a request, the most amount of time the service will wait for a response. By default, the deadline for a fetch is 5 seconds. You can adjust the default deadline for requests using the urlfetch.set_default_fetch_deadline()function.

您可以为请求设置截止时间,即服务等待响应的最长时间。默认情况下,获取的截止时间为 5 秒。您可以使用该urlfetch.set_default_fetch_deadline()功能调整请求的默认截止日期。

And this pagelists the default timeout values:

此页面列出了默认的超时值:

Currently, there are several errors named DeadlineExceededError for the Python runtime:

  • google.appengine.runtime.DeadlineExceededError: raised if the overall request times out, typically after 60 seconds, or 10 minutes for task queue requests.
  • google.appengine.runtime.apiproxy_errors.DeadlineExceededError: raised if an RPC exceeded its deadline. This is typically 5 seconds, but it is settable for some APIs using the 'deadline' option.
  • google.appengine.api.urlfetch_errors.DeadlineExceededError: raised if the URLFetch times out.

目前,Python 运行时有几个名为 DeadlineExceededError 的错误:

  • google.appengine.runtime.DeadlineExceededError:如果整个请求超时,通常在 60 秒后或任务队列请求 10 分钟后引发。
  • google.appengine.runtime.apiproxy_errors.DeadlineExceededError: 如果 RPC 超过其截止日期,则引发。这通常是 5 秒,但对于某些使用 'deadline' 选项的 API,它是可设置的。
  • google.appengine.api.urlfetch_errors.DeadlineExceededError: 如果 URLFetch 超时则引发。

回答by gosharplite

For Go, you might want to try below code.

对于 Go,您可能想尝试以下代码。

// createClient is urlfetch.Client with Deadline
func createClient(context appengine.Context, t time.Duration) *http.Client {
    return &http.Client{
        Transport: &urlfetch.Transport{
            Context:  context,
            Deadline: t,
        },
    }
}

Here is how to use it.

这是如何使用它。

// urlfetch
client := createClient(c, time.Second*60)

回答by gosharplite

It seems short but you have to know that the timeout of a request on GAE is around 30 seconds. As you probably need to do some operations on the response of your urlfetch, there's no need to have a timeout more than 10 seconds I think.

看起来很短,但您必须知道 GAE 上的请求超时约为 30 秒。由于您可能需要对 urlfetch 的响应进行一些操作,因此我认为没有必要超时超过 10 秒。