在gevent+tornado设置中抑制Greenlet异常

2024-09-29 00:20:22 发布

您现在位置:Python中文网/ 问答频道 /正文

当通过gevent的pywsgi服务器运行tornado的wsgi应用程序时,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()

Tags: importselfwebapp应用程序wsgiinitdef
1条回答
网友
1楼 · 发布于 2024-09-29 00:20:22

你试过运行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()

输出:

^{pr2}$

相关问题 更多 >