1

当通过 gevent 的 pywsgi 服务器运行 tornado 的 WSGIApplication 时,greenlets 中的异常被抑制并且不显示在标准错误/输出中。我看了又看,找不到为什么会这样。

这是一个小测试应用程序来演示:

#!/usr/bin/env python

import gevent.monkey
gevent.monkey.patch_all()

import gevent.wsgi
import tornado.web
import tornado.wsgi

class MainHandler(tornado.web.RequestHandler):
  def prepare(self):
    # this next line will cause a NameError
    a = i_dont_exist_here

class App(tornado.wsgi.WSGIApplication):
  def __init__(self):
    tornado.wsgi.WSGIApplication.__init__(self, [(r"/", MainHandler)])

if __name__ == '__main__':
  gevent.wsgi.WSGIServer(('', 80), App()).serve_forever()
4

1 回答 1

0

你试过跑步tornado.options.parse_command_line()吗?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gevent.monkey
gevent.monkey.patch_all()

import gevent.wsgi
import tornado.web
import tornado.wsgi

from tornado.options import parse_command_line
tornado.options.parse_command_line()

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        raise Exception("Something terrible happened here")

class App(tornado.wsgi.WSGIApplication):
    def __init__(self):
        tornado.wsgi.WSGIApplication.__init__(self, [(r"/", MainHandler)])

if __name__ == '__main__':
    gevent.wsgi.WSGIServer(('', 8000), App()).serve_forever()

输出:

[E 130819 00:11:27 web:1228] Uncaught exception GET / (127.0.0.1)
    <tornado.wsgi.HTTPRequest object at 0x0000000003009A20>
    Traceback (most recent call last):
      File "C:\Python27\lib\site-packages\tornado\web.py", line 1141, in _when_complete
        callback()
      File "C:\Python27\lib\site-packages\tornado\web.py", line 1162, in _execute_method
        self._when_complete(method(*self.path_args, **self.path_kwargs),
      File "Test.py", line 16, in get
        raise Exception("Something terrible happened here")
    Exception: Something terrible happened here
127.0.0.1 - - [2013-08-19 00:11:27] "GET / HTTP/1.1" 500 93 "-" "Mozilla/5.0 (Wi
ndows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"
于 2013-08-18T22:11:42.350 回答