GAE应用在本地引发DeadlineExceederError并部署DownloadError

2024-04-19 14:54:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这很奇怪。标题说了大部分,我的代码应该说其余的。这是我的main.py文件:

from google.appengine.api import urlfetch
import webapp2
import jinja2

import json
import os

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))


class MainPage(webapp2.RequestHandler):
    def get(self):
        response = urlfetch.fetch("http://localhost:8080/api/helloworld?name=totty", method=urlfetch.GET)
        if response.status_code == 200:
            result = json.loads(response.content)

        template_values = {'response': result['msg']}
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))


app = webapp2.WSGIApplication(
    [('/', MainPage)],
    debug=True)

这是我的api.py文件:

^{pr2}$

如果我的app.yaml文件有帮助:

application: stacksort
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /api/.*
  script: api.app
- url: /.*
  script: main.app

libraries:
- name: webapp2
  version: latest
- name: jinja2
  version: latest

即使我将deadline=30添加到urlfetch调用中,也不会发生任何更改。我使用httpie和JQuery测试了这个API,它工作得非常好,并且在5秒内返回。在

我看了其他的问题,但我还是在黑暗中蹒跚而行。如有帮助、提示或重构,将不胜感激。在

我计划添加对StackEchange搜索API的调用,所以我怀疑这个问题可能也会出现。如果有更好的方法,请告诉我。谢谢。在


Tags: 文件namepyimportapijsonappjinja2
2条回答

你正在尝试从你的应用程序中获取一个URL,这在googleappengine上是非常不推荐的。在

在本地,您将无法调用开发服务器,因为它一次只为一个请求提供服务。未观察到多线程。在

注意:新的实验性开发服务器现在可以一次处理多个请求。在

Multithreaded serving for better performance for complex applications and more correct semantics e.g. accessing your own application through urlfetch no longer deadlocks.

在生产环境中,GAE阻止fetch服务调用同一个应用程序。在

To prevent an app from causing an endless recursion of requests, a request handler is not allowed to fetch its own URL. It is still possible to cause an endless recursion with other means, so exercise caution if your app can be made to fetch requests for URLs supplied by the user.

升级SDK时,我注意到DevServer page中添加了以下内容:

Note: dev_appserver.py can only serve one request at a time. If your application makes URL fetch requests to itself while processing a request, these requests will fail when using the development web server. (They will not fail when running on App Engine.) To test such requests, you can run a second instance of dev_appserver.py on a different port, then code your application to use the other server when making requests to itself.

所以我想这解决了我的问题(或者至少给出了一个令人满意的解释)。在

相关问题 更多 >