google-app-engine - 在 Google App Engine 上部署 Tornado

标签 google-app-engine tornado

我正在尝试将我的 python 定价模块部署到 GAE,该模块将产品详细信息(字符串)作为参数。 Tornado 包装器在本地主机 (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

如何纠正这个问题?

最佳答案

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 指令中引用应用程序来告诉 GAE 在哪里可以找到它:

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

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

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

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

关于google-app-engine - 在 Google App Engine 上部署 Tornado ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45316728/

相关文章:

python - 将 GAE 应用程序迁移到 Python 2.7 后出现 wsgi.py 错误

python - Tornado 和基于Python的守护进程之间的最佳通信方式是什么?

python - 在 AWS 上使用 Tornado 托管 Bokeh 服务器

javascript - 上传从网络摄像头拍摄的图片

python - 如何在tornado.wsgi.WSGIContainer中使用异步tornado API?

java - 谷歌应用程序引擎 Java : Where is the source code of "CompiledCursor"?

python - Google App Engine get_serving_url 不再放大图像

java - "Detecting"并在 GAE 中加载 "plugins"

javascript - 更改 Google map API 的输入

python - 收到通知后从服务器向客户端发送消息(Tornado+websockets)