【问题标题】:How do I solve the error of the official canonical "Hello, world" example app of Tornado?如何解决 Tornado 官方规范的“Hello, world”示例应用程序的错误?
【发布时间】:2023-10-22 07:32:01
【问题描述】:
My python version is 2.7.2

python 由 uwsgi 运行 我的 nginx 配置是

location /{

uwsgi_pass 127.0.0.1:8888;

include uwsgi_params;

}

app.py

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    application.listen(9090)
    tornado.ioloop.IOLoop.instance().start()

然后我运行“我运行”uwsgi -s :9090 -w app”

但是会报错

[pid: 28719|app: 0|req: 21/21] 118.207.180.64 () {38 vars in 716 bytes} [Sun Mar 23 22:44:34 2014] GET / => 在 0 中生成了 0 个字节毫秒 (HTTP/1.1 500) 0 个字节中的 0 个标头(核心 0 上的 0 个开关) AttributeError: application instance has no call method

如何解决?

【问题讨论】:

  • 您可以链接或粘贴您的 uwsgi 配置 (/etc/uwsgi/your-app.ini) 吗?

标签: python nginx centos tornado uwsgi


【解决方案1】:
import tornado.web
import tornado.wsgi
import wsgiref.simple_server

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.wsgi.WSGIApplication([
        (r"/", MainHandler),
    ])
    server = wsgiref.simple_server.make_server('', 8888, application)
    server.serve_forever()

(来自official docs

【讨论】:

  • 不,仍然是错误并显示“Internal Server Error” error.log 是“--- no python application found, check your startup log for errors ---”
  • 在另一边,我没有像 my_app.ini 文件这样的 xxx.ini
  • 要将 Tornado 与 uwsgi 一起使用,请将 application = 行移出 if __name__ == "__main__" 块(但不是处理 server 的行 - 删除它们或将它们留在 __main__ 块中)。但是,在这种模式下使用 Tornado 会禁用其许多最有趣的功能,因此您可能想要更改 nginx 配置而不是 Tornado 代码(我在对这个问题的回答中对此进行了解释)
【解决方案2】:

Tornado 是一个 HTTP 服务器,而不是一个 WSGI 容器(它可以用作 WSGI 容器,就像 x3al 的回答一样,但是你失去了它的一些最有趣的特性)。使用 nginx proxy_pass 选项而不是 uwsgi_pass;一个完整的 nginx 配置示例可以在http://www.tornadoweb.org/en/stable/overview.html#running-tornado-in-production找到。

【讨论】:

    【解决方案3】:

    问题是您将龙卷风应用程序作为 uwsgi 应用程序传递。这是修复方法,使用 tornado 4.0+ 中可用的WSGIAdapter

    import tornado.ioloop
    import tornado.web
    import tornado.wsgi
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("Hello, world")
    
    
    t_application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    application = tornado.wsgi.WSGIAdapter(app)  # For wsgi layer
    
    if __name__ == "__main__":
    
        t_application.listen(9090)
        tornado.ioloop.IOLoop.instance().start()
    

    但是通过 WSGI 适配器使用 Tornado,您会失去 Tornado 提供的一些最有趣的功能。在 4.0 版本之前,WSGIApplication 可用。

    【讨论】:

      最近更新 更多