2010-01-29 255 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

相關問題