【问题标题】:deploying tornado on Google App Engine在 Google App Engine 上部署龙卷风
【发布时间】:2017-07-26 03:05:17
【问题描述】:

我正在尝试将我的以产品详细信息(字符串)作为参数的 python 定价模块部署到 GAE。龙卷风包装器在 localhost (localhost:8888/?q=) 上运行良好,但在 GAE 上出现服务器错误 500。

Pricing-OOP.py 文件中的代码:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        q = self.get_query_argument("q")
        res = Pricing(q).pricing()
        self.write(json.dumps(res))

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ],debug=True)   

if __name__ == '__main__':
    Pickleload()
    app = make_app()
    container = tornado.wsgi.WSGIContainer(app)
    http_server = tornado.httpserver.HTTPServer(container)  
    http_server.listen(8888)
    tornado.ioloop.IOLoop.current().start()

app.yaml 文件:

service: tornado
runtime: python27
threadsafe: no

handlers:
- url: /.*
  script: Pricing-OOP.py

gcloud 应用日志尾如下:

2017-07-26 03:03:30 tornado[20170726t082447]  "GET / HTTP/1.1" 500
2017-07-26 03:03:30 tornado[20170726t082447]  "GET /favicon.ico HTTP/1.1" 500
2017-07-26 03:03:33 tornado[20170726t082447]  "GET / HTTP/1.1" 500
2017-07-26 03:03:34 tornado[20170726t082447]  "GET /favicon.ico HTTP/1.1" 500

我该如何纠正这个问题?

【问题讨论】:

    标签: google-app-engine tornado


    【解决方案1】:

    the Tornado docs 中有一些关于在 Google App Engine 上部署的注意事项。

    特别是,GAE 上的 Tornado 应用程序必须作为 WSGI 应用程序运行。你不能在本地机器上做开放端口之类的事情, 不幸的是,它还阻止了 Tornado 的异步方面的使用(这通常是首先使用它的主要驱动力)。

    在您的情况下,您应该只创建 WSGIAdapter,而不是自己创建 HttpServer

    # Pricing-OOP.py
    # ...
    def make_app():
        return tornado.web.Application([
            (r"/", MainHandler),
        ],debug=False)
    
    application = make_app()
    application = tornado.wsgi.WSGIAdapter(application)
    

    然后,您可以通过在配置文件的 script 指令中引用 application 来告诉 GAE 在哪里找到它:

    # app.yaml
    # ...
    handlers:
    - url: /.*
      script: Pricing-OOP.application
    

    因为你现在是一个“纯”的 WSGI 应用程序,你需要在容器中运行它。 Google Cloud SDKdev_appserver.py 中包含一个开发服务器,可用于托管您的应用程序:

    $ dev_appserver.py 。 # 在包含 app.yaml 的目录中

    完成后,您可以在本地和 GAE 实例中运行相同的应用程序代码。

    【讨论】:

    • 还要注意debug=False 很重要,因为如果您将其设置为True,Tornado 将尝试打开本地主机端口,并且您将在启动时收到 AccessDenied 异常。跨度>
    猜你喜欢
    • 2015-05-01
    • 2023-03-02
    • 2020-08-18
    • 2012-02-11
    • 2013-03-11
    • 2020-04-19
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多