tornado.wsgi.WSGIApplication问题:__ call__正好需要3个参数(给定2个)

时间:2014-01-31 16:13:27

标签: python google-app-engine tornado wsgi

作为项目的一部分,我一直在尝试移植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)

我无法做出正面或反面的事情,谷歌也没有找到任何有同样问题的人(除了那些推荐在Tornado文件中捣乱的人,这对App Engine AFAIK无济于事)。有没有其他人看过这个错误,或者可以发现当默认应用程序没有时WSGIApplication崩溃的原因?

1 个答案:

答案 0 :(得分:2)

遗憾的是,名称“application”在这里被重载 - tornado.web.Application和WSGIApplication在它们与服务器的接口上有所不同。 tornado.web.Application可以由tornado HTTPServer使用,但WSGIApplication必须在WSGI容器中运行。在app引擎部署中,您只需在配置文件中直接提及您的WSGIApplication实例,而不提及tornado.httpserver。要使用WSGIApplication来修改tornado.httpserver,请使用tornado.wsgi.WSGIContainer:HTTPServer(WSGIContainer(application()))

相关问题