tornado.wsgi.WSGIA应用问题:__call__只需要3个参数(给出2个参数) [英] tornado.wsgi.WSGIApplication issue: __call__ takes exactly 3 arguments (2 given)

查看:128
本文介绍了tornado.wsgi.WSGIA应用问题:__call__只需要3个参数(给出2个参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import wsgiref 
导入tornado.wsgi
导入tornado.web
导入tornado.httpserver
导入os
导入处理程序从tornado.options导入处理程序
导入定义,选项

define(port,default = 8000,help =在给定端口上运行,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__),st atic),
debug = True)

返回tornado.web.Application(处理程序,**设置)

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 
导入龙卷风。 wsgi
导入tornado.web
导入tornado.httpserver
导入os
导入处理程序从tornado.options导入的
导入定义,选项

定义(port,default = 8000,help =在给定端口上运行,type = int)

def application():
handlers = [(r/,Handlers .HainHandler),
(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)

返回tornado.wsgi.WSGIApplication(处理程序,**设置)

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]未捕获异常,关闭连接。 
Traceback(最近的最后一次调用):
文件/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/iostream .py,第354行,在包装中
callback(* args)
文件/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64 .egg / tornado / stack_context.py,第331行,包装在
raise_exc_info(exc)
文件中/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2。 (* args,** kwargs)
文件/ usr / local / lib / python2中的7-linux-x86_64.egg / tornado / stack_context.py,第302行。 7 / dist-packages / tornado-3.2-py2.7-linux-x86_64.egg / tornado / httpserver.py,第328行,位于_on_headers
self.request_callback(self._request)
TypeError: __call __()只需要3个参数(给出2个)
[E 140131 10:02:18 ioloop:491]回调函数中的异常< functools.partial对象在0xf6e3ecfc>
Traceback(最近一次调用最后一次):
文件/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/ioloop .py,第477行,在_run_callback
callback()
文件中/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg /tornado/stack_context.py,第331行,包装在
raise_exc_info(exc)
文件中/usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7- linux-x86_64.egg / tornado / stack_context.py,第302行,包装在
ret = fn(* args,** kwargs)
文件中/usr/local/lib/python2.7/ (* args)
文件/ usr / local / dist-packages / tornado-3.2-py2.7-linux-x86_64.egg / tornado / iostream.py,第354行。 lib / python2.7 / dist-packages / tornado-3.2-py2.7-linux-x86_64.egg / tornado / stack_context.py,第331行,包装在
raise_exc_info(exc)
文件 /usr/local/lib/python2.7/dist-packages/tornado-3.2-py2.7-linux-x86_64.egg/tornado/stack_cont ext.py,第302行,包装在
ret = fn(* args,** kwargs)
文件中/usr/local/lib/python2.7/dist-packages/tornado-3.2- py2.7-linux-x86_64.egg / tornado / httpserver.py,行328,在_on_headers
self.request_callback(self._request)
TypeError:__call __()只需要3个参数)

我无法做出正面或反面的评论,而谷歌也没有发现任何人同样的问题(除了那些推荐在Tornado文件中使用的人,这对App Engine AFAIK无助)。有没有其他人看过这个错误,或者可以发现为什么WSGIApplication在默认应用程序不会崩溃的原因?

解决方案

应用程序不幸被重载 - tornado.web.Application和WSGIApplication在它们与服务器的接口上有所不同。 tornado.web.Application可以被龙卷风HTTPServer使用,但WSGIApplication必须在WSGI容器中运行。在应用程序引擎部署中,您只需在配置文件中直接提及您的WSGIApplication实例,而不提及tornado.httpserver。要使用将WSGIApplication修改为tornado.httpserver,请使用tornado.wsgi.WSGIContainer: HTTPServer(WSGIContainer(application()))


As part of a project, I've been trying to port a Tornado server to work on the Google App Engine. Since the App Engine doesn't implement the asynchronous functions of normal Tornado, I've been trying to convert the main Application to a WSGIApplication. The normal main code works fine (forgive the imports and formatting, it's a mess from trying to follow other examples):

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()

I can access the web pages, browse the site, works great. If I change line 24 to return a tornado.wsgi.WSGIApplication, like thus:

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()

it also runs just fine. However, when I try to access any of the web pages, it gives me the following error:

[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)

Which I can't make heads or tails of, and Google hasn't turned up anyone with the same problem (except for the guy who recommended mucking around in the Tornado files, which won't help on the App Engine AFAIK). Has anyone else seen this error before, or can spot why the WSGIApplication crashes when the default Application does not?

解决方案

The name "application" is unfortunately overloaded here - tornado.web.Application and WSGIApplication differ in their interface to the server. tornado.web.Application can be used by a tornado HTTPServer, but WSGIApplication must be run in a WSGI container. In app engine deployment, you'd just mention your WSGIApplication instance directly in the config file, with no mention of tornado.httpserver. To use adapt a WSGIApplication to tornado.httpserver, use a tornado.wsgi.WSGIContainer: HTTPServer(WSGIContainer(application()))

这篇关于tornado.wsgi.WSGIA应用问题:__call__只需要3个参数(给出2个参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆