2010-01-29 256 views
15

我想让Django(在GAE之上)从另一个Web服务获取数据。我经常打错误是这样的:如何在Google App Engine中为urlfetch设置超时时间?

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

感觉,就好像它会超时仅12秒后(我不知道,但它是真的很短)。

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

回答

23

您可以使用fetch function的参数deadline进行设置。从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.


编辑:看起来像这样现在已经改变。从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.

而且this page列出了默认的超时值:

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.
+0

好的,我可以知道为什么你决定用Java回答这个问题,当OP明确表示他使用Django时?您没有提供python等价物:( – kassold

+0

其他人在我给了它两年后编辑了我的答案,并由于某种原因添加到Java代码段中......如果您查看第一行中的超链接,它们链接到Python文档在这里给出了一个Python例子Alex Young的回答 –

+0

你链接到的文档不再包含任何提及最多60秒的时间,这个限制是否被弃用? – conradlee

-1

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

27

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

只需导入urlfetch,然后在你的代码做任何事情之前确定的最后期限:

from google.appengine.api import urlfetch 

urlfetch.set_default_fetch_deadline(60) 
+0

在[Version 1.5.3] (http://code.google.com/p/googleappengine/wiki/SdkReleaseNotes#Version_1.5.3_-_August_17,_2011) –

7

对于去,你可能会想尝试下面的代码。

// 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) 
+0

我意识到这个评论是从2013年开始的,但API改变了,urlfetch,Transport不再有截止日期要设置最后期限,请在上下文中将其设置为:'ctx,_:= context.WithDeadline(context,time.Second * 60)'。 此方法的问题在于您o限制所有后续请求的可用时间。我的意思是,如果您在任务中轮询bigquery(超时时间为10分钟)并将截止日期设置为60秒,则实际上将轮询周期限制为60秒,因为截止日期是在上下文中设置的客户端传递给BigQuery。 – gabrielf

相关问题