我有一个基本的应用程序。我使用twitter api 1.1和python。当我在本地运行时,我没有得到任何错误,但在部署后我得到DeadlineExceededError错误。这是log 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
这是main.py
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)
你能帮助我吗?如果您想查看任何代码,请告诉我。
答案 0 :(得分:1)
正如 Wooble this评论中所述,Stack Overflow问题包含对您看到的 DeadlineExceededError 的可能答案。
我会尝试解释答案,以便它可以帮助您解决问题。
您使用普通的Python库 urllib , urllib2 和 httplib 在App Engine上获取互联网资源。但是,在Google App Engine上,这些库使用Google URL Fetch服务获取Internet资源。这意味着其他一组服务器(实际托管您的应用程序的服务器除外)将为您提取数据。
使用URL Fetch服务在App引擎上获取资源时,如果请求未在规定的截止日期内完成(应用程序指定或默认值 60 s ),则DeadlineExceededException为抛出。
引用Dealing with DeadlineExceededError
使用URLFetch向外部URL发出请求也可以产生 如果目标网站具有性能,则DeadlineExceededErrors 问题或通常需要超过60秒才能回复。记录 DeadlineExceededErrors的堆栈跟踪应该包含对的调用 在这些情况下,URLFetch库。
Twitter API请求可能未在规定的截止日期内完成。请尝试以下方法之一:
答案 1 :(得分:0)
问题是您的整体请求需要60秒以上才能完成。这不是因为你使用了urlfetch - 通常会在几秒钟内超时,如果它超时,你可以在60s的限制范围内处理错误。
问题实际上是您发出 18 urlfetch请求。由于每个请求可能需要几秒钟,因此加起来并达到60秒限制非常容易。
您可能需要重新架构main.py并在任务队列中执行实际的URL提取,并将结果存储在数据存储区中。任务队列可以运行更长时间。
在搜索返回后,您需要某种第二种处理程序来检查任务的状态。