【问题标题】:tornado.wsgi.WSGIApplication issue: __call__ takes exactly 3 arguments (2 given)tornado.wsgi.WSGIApplication 问题:__call__ 正好需要 3 个参数(给定 2 个)
【发布时间】:2014-01-31 16:13:27
【问题描述】:

作为项目的一部分,我一直在尝试移植 Tornado 服务器以在 Google App Engine 上工作。由于 App Engine 没有实现普通 Tornado 的异步功能,所以我一直在尝试将主应用程序转换为 WSGIApplication。正常的主代码工作正常(原谅导入和格式化,尝试遵循其他示例会一团糟):

import wsgiref
import tornado.wsgi
import tornado.web
import tornado.httpserver
import os
import Handlers
from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

def application():
    handlers=[(r"/", Handlers.MainHandler),
            (r"/Login", Handlers.LoginHandler),
            (r"/ViewHistory",Handlers.ViewHistoryHandler),
            (r"/UploadFile", Handlers.UploadHandler),
            (r"/Index", Handlers.IndexHandler),
            (r"/About", Handlers.AboutHandler),
            (r"/Profile", Handlers.ProfileHandler)]

    settings=dict(template_path=os.path.join(os.path.dirname(__file__), "templates"),
                static_path=os.path.join(os.path.dirname(__file__), "static"),
                debug=True)

    return tornado.web.Application(handlers, **settings)

def main():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

我可以访问网页,浏览网站,效果很好。如果我将第 24 行更改为返回 tornado.wsgi.WSGIApplication,如下所示:

import wsgiref
import tornado.wsgi
import tornado.web
import tornado.httpserver
import os
import Handlers
from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

def application():
    handlers=[(r"/", Handlers.MainHandler),
            (r"/Login", Handlers.LoginHandler),
            (r"/ViewHistory",Handlers.ViewHistoryHandler),
            (r"/UploadFile", Handlers.UploadHandler),
            (r"/Index", Handlers.IndexHandler),
            (r"/About", Handlers.AboutHandler),
            (r"/Profile", Handlers.ProfileHandler)]

    settings=dict(template_path=os.path.join(os.path.dirname(__file__), "templates"),
                static_path=os.path.join(os.path.dirname(__file__), "static"),
                debug=True)

    return tornado.wsgi.WSGIApplication(handlers, **settings)

def main():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

它也运行得很好。但是,当我尝试访问任何网页时,都会出现以下错误:

[E 140131 10:02:18 iostream:357] Uncaught exception, closing connection.
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/iostream.py", line 354, in wrapper
        callback(*args)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 331, in wrapped
        raise_exc_info(exc)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 302, in wrapped
        ret = fn(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/httpserver.py", line 328, in _on_headers
        self.request_callback(self._request)
    TypeError: __call__() takes exactly 3 arguments (2 given)
[E 140131 10:02:18 ioloop:491] Exception in callback <functools.partial object at 0xf6e3ecfc>
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/ioloop.py", line 477, in _run_callback
        callback()
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 331, in wrapped
        raise_exc_info(exc)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 302, in wrapped
        ret = fn(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/iostream.py", line 354, in wrapper
        callback(*args)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 331, in wrapped
        raise_exc_info(exc)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_context.py", line 302, in wrapped
        ret = fn(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/httpserver.py", line 328, in _on_headers
        self.request_callback(self._request)
    TypeError: __call__() takes exactly 3 arguments (2 given)

我无法确定这一点,而且 Google 还没有发现任何有同样问题的人(除了建议在 Tornado 文件中乱搞的人,这对 App Engine AFAIK 没有帮助)。有没有其他人以前见过这个错误,或者可以发现为什么 WSGIApplication 在默认应用程序没有崩溃时崩溃?

【问题讨论】:

    标签: python google-app-engine tornado wsgi


    【解决方案1】:

    不幸的是,“应用程序”这个名字在这里被重载了——tornado.web.Application 和 WSGIApplication 在它们与服务器的接口上有所不同。 tornado.web.Application 可以被 tornado HTTPServer 使用,但是 WSGIApplication 必须在 WSGI 容器中运行。在应用引擎部署中,您只需在配置文件中直接提及您的 WSGIApplication 实例,而不提及 tornado.httpserver。要使用将 WSGIApplication 适配到 tornado.httpserver,请使用 tornado.wsgi.WSGIContainer:HTTPServer(WSGIContainer(application()))

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 1970-01-01
      • 2017-06-21
      • 2012-03-15
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      相关资源
      最近更新 更多