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

发布于 2024-08-19 10:38:39 字数 457 浏览 3 评论 0原文

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

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

方法:获取

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

异常类型:下载错误

异常值:ApplicationError:2 超时

异常位置:_get_fetch_result 第 325 行中的 /google_appengine/google/appengine/api/urlfetch.py​​

感觉好像只有 12 秒后才会超时(我不确定,但确实很短) 。

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

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

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

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

Question: how can I set a longer timeout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦太阳 2024-08-26 10:38:39

鉴于这是一个 Python 问题,我想我应该为遇到此问题的任何人提供一个 Python 答案。

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

from google.appengine.api import urlfetch

urlfetch.set_default_fetch_deadline(60)

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

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

from google.appengine.api import urlfetch

urlfetch.set_default_fetch_deadline(60)
Saygoodbye 2024-08-26 10:38:39

您可以使用 deadline 参数进行设置获取函数。来自文档

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


编辑:看起来现在已经改变了。来自此处

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

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

目前,Python 运行时存在多个名为 DeadlineExceededError 的错误:

  • google.appengine.runtime.DeadlineExceededError:如果整个请求超时(通常在 60 秒后,对于任务队列请求则为 10 分钟),则会引发此错误。
  • google.appengine.runtime.apiproxy_errors.DeadlineExceededError:如果 RPC 超出其截止时间,则会引发该错误。这通常是 5 秒,但对于某些 API 来说可以使用“截止日期”选项进行设置。
  • google.appengine.api.urlfetch_errors.DeadlineExceededError:如果 URLFetch 超时,则会引发该错误。

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

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.


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.

And this page lists 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.
旧人九事 2024-08-26 10:38:39

对于 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,
        },
    }
}

以下是如何使用它。

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

For Go, you might want to try below code.

// 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)
慢慢从新开始 2024-08-26 10:38:39

看似很短,但要知道GAE上的请求超时时间在30秒左右。由于您可能需要对 urlfetch 的响应执行一些操作,因此我认为超时不需要超过 10 秒。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文