如何用gevent捕获异常?

时间:2017-09-04 14:29:04

标签: python python-3.x flask exception-handling gevent

我在gevent

之上运行flask以下服务器
import gevent.monkey
gevent.monkey.patch_all()
import flask
import gevent.wsgi

app = flask.Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

gevent.wsgi.WSGIServer(('127.0.0.1', 5000), app, keyfile='my.key', certfile='my.cer').serve_forever()

调用https://127.0.0.1:5000时运行正常但与http://127.0.0.1:5000崩溃。原因是在HTTPS上下文中没有正确处理HTTP调用:

Traceback (most recent call last):
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\greenlet.py", line 536, in run
    result = self._run(*self.args, **self.kwargs)
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\baseserver.py", line 26, in _handle_and_close_when_done
    return handle(*args_tuple)
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\server.py", line 173, in wrap_socket_and_handle
    ssl_socket = self.wrap_socket(client_socket, **self.ssl_args)
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\_ssl3.py", line 646, in wrap_socket
    ciphers=ciphers)
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\_ssl3.py", line 229, in __init__
    raise x
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\_ssl3.py", line 225, in __init__
    self.do_handshake()
  File "C:\Users\aaa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gevent\_ssl3.py", line 549, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: HTTP_REQUEST] http request (_ssl.c:645)
Mon Sep  4 16:19:01 2017 <Greenlet at 0x4631c60: _handle_and_close_when_done(<bound method StreamServer.wrap_socket_and_handle , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed]  object, fd=-1, )> failed with SSLError

在这样的循环中捕获异常的正确方法是什么?{<1}}?

我尝试了天真的

.serve_forever()

但它没有被抓住(我的手挥动的印象是try: gevent.wsgi.WSGIServer(('127.0.0.1', 5000), app, keyfile='my.key', certfile='my.cer').serve_forever() except Exception as e: print("got exception {e}".format(e=e)) / try对尚未到位(并且永远不会)except循环正在运行时)

1 个答案:

答案 0 :(得分:0)

您可以生成一个greenlet并为其附加异常处理程序:

g = gevent.spawn(WSGIServer(...).serve_forever)
def exception_callback(g):
    """Process gevent exception"""
    try:
        g.get()
    except Exception as exc:
        pass

g.link_exception(exception_callback)