2013-10-02 84 views
0

我有一個基本的應用程序。我使用twitter api 1.1和python。雖然我在本地運行,但沒有發生錯誤,但部署後我得到了DeadlineExceededError錯誤。這裏是日誌MSJ:服務器錯誤和超時超時錯誤

Traceback (most recent call last): 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle 
    result = handler(dict(self._environ), self._StartResponse) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/apps/s~tweetllrio/1.370638782538988919/main.py", line 52, in post 
    ''+username+'&max_id='+str(max_id)+'&count=200') 
    File "libs/oauth2/__init__.py", line 676, in request 
    uri = req.to_url() 
    File "libs/oauth2/__init__.py", line 421, in to_url 
    query = parse_qs(query) 
    File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urlparse.py", line 382, in parse_qs 
    for name, value in parse_qsl(qs, keep_blank_values, strict_parsing): 
    File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urlparse.py", line 423, in parse_qsl 
    name = unquote(nv[0].replace('+', ' ')) 
    File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urlparse.py", line 337, in unquote 
    if _is_unicode(s): 
DeadlineExceededError 

class Search(webapp2.RequestHandler): 

    def post(self): 
     username = self.request.get("contenta") 
     word = self.request.get("contentc") 
     header, response = client.request(
      'https://api.twitter.com/1.1/statuses/user_timeline' 
      '.json?include_entities=true&screen_name='+username+'&count=1') 
     name = json.loads(response)[0]["user"]["name"] 
     image = json.loads(response)[0]["user"]["profile_image_url"] 
     max_id = json.loads(response)[0]["id"] 
     count = 0 
     tweets = [] 
     while count < 18: 
      header, response = client.request(
       'https://api.twitter.com/1.1/statuses/user_timeline' 
       '.json?include_entities=true&include_rts=false&screen_name=' 
       ''+username+'&max_id='+str(max_id)+'&count=200') 
      for index in range(len(json.loads(response))-1): 
       if word in json.loads(response)[index]["text"]: 
        tweets.append(json.loads(response)[index]["text"]) 
      max_id = json.loads(response)[len(json.loads(response))-1]["id"] 
      count += 1 

     template = JINJA_ENVIRONMENT.get_template('index.html') 
     self.response.write(template.render(
      {"data": tweets[::-1], "name": name, "image": image, "da":len(tweets)}) 
     ) 
class MainPage(webapp2.RequestHandler): 

    def get(self): 

     template = JINJA_ENVIRONMENT.get_template('index.html') 
     self.response.write(template.render({})) 

application = webapp2.WSGIApplication([ 
    ('/', MainPage), 
    ('/search', Search), 
    ('/add', AddUSer), 
], debug=True) 

請你能幫我這是main.py?如果你想看任何代碼,請告訴我。

+0

[GAE上的Twitter流式傳輸](http://stackoverflow.com/questions/14495868/twitter-streaming-on-gae) – geoffspear

+0

我加了main.py,夠了嗎?請給我一個解決方法。 –

+0

Wooble我沒有理解任何東西:// –

回答

1

正如Wooblethis堆棧溢出問題在評論中提及了含有一個可能的答案給DeadlineExceededError你看。

我會盡力解釋答案,以便它可以幫助您解決問題。

您使用正常的Python庫的urllib的urllib2httplib的在App引擎獲取網絡資源。但是,在Google App Engine上,這些庫使用Google URL抓取服務獲取Internet資源。這意味着其他一些服務器(除了實際託管您的應用程序之外)將爲您獲取數據。

使用URL提取服務在App引擎上獲取資源時,如果請求沒有在規定的截止日期內完成(應用程序指定的或默認值爲60 s),則引發DeadlineExceededException。

引述Dealing with DeadlineExceededError

發出請求使用的URLFetch也能產生 DeadlineExceededErrors如果目標網站是有性能 問題或一般需要超過60秒回覆外部URL。在這些情況下,DeadlineExceededErrors的日誌記錄 堆棧跟蹤應該包含對 URLFetch庫的調用。

可能是twitter API請求沒有在規定的期限內完成。請嘗試執行以下操作之一:

  1. 以異步方式獲取Twitter資源。
  2. 指定一個明確的截止時間大於60秒(如120秒)並檢查請求是否成功完成。我不會推薦這種方法,因爲這純粹是上下文的應用程序運行的場景,更多地基於試錯技術。
+0

非常感謝你,真是太棒了。現在我無法獲得解決方案。你能解釋一點點嗎?我應該怎麼做才能克服這種情況呢? –

+1

我還沒有在Google App引擎上工作過,但是有關URL Fetch服務的這個鏈接:https://developers.google.com/appengine/docs/python/urlfetch/提到了異步API的可用性以及它們的使用方式操作。 –

0

問題是您的整體請求需要60秒以上才能完成。這不是因爲您使用的是urlfetch - 通常在幾秒鐘內超時,如果超時,您可以在60秒內處理錯誤。

這個問題的確是你發佈了 urlfetch請求的事實。由於每個請求可能需要幾秒鐘的時間,因此這很容易累加並達到60年代的限制。

您可能需要重新構建main.py並在Task Queue中執行實際的URL提取,並將結果存儲在數據存儲中。任務隊列可以運行更長時間。

您需要第二個某種處理程序才能在搜索返回後檢查任務的狀態。

+0

非常感謝,我會盡力。 –