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

GRe*_*Rex 18 python django google-app-engine timeout

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

ApplicationError:2超时请求

方法:GET

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

异常类型:DownloadError

异常值:ApplicationError:2超时

例外位置:_get_fetch_result中的/google_appengine/google/appengine/api/urlfetch.py​​,第325行

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

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

Ale*_*ung 27

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

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

from google.appengine.api import urlfetch

urlfetch.set_default_fetch_deadline(60)
Run Code Online (Sandbox Code Playgroud)


Mar*_*ell 24

您可以使用fetch函数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超时,则引发.


gos*_*ite 7

对于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,
        },
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是如何使用它.

// urlfetch
client := createClient(c, time.Second*60)
Run Code Online (Sandbox Code Playgroud)