在gevent +龙卷风设置中抑制了Greenlet例外

时间:2013-08-16 15:18:09

标签: python tornado gevent

当通过gevent的pywsgi服务器运行龙卷风的WSGIApplication时,greenlet中的异常被抑制,并且不会出现在标准错误/输出中。我看了看,但却找不到为什么会发生这种情况。

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

#!/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()

1 个答案:

答案 0 :(得分: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"