2013-10-02 86 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

非常感谢,我会尽力。 –