Flask服务器发送事件套接字异常

问题描述 投票:5回答:4

我正在考虑使用SSE将新数据推送到客户端并使用Flot(javascript图表库)显示“实时”更新。我的服务器在python Flask框架上运行,我已经想出如何将数据推送到客户端,但是一旦我离开页面就会出现问题:

Exception happened during processing of request from ('127.0.0.1', 38814)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 582, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
    self.wfile.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

我理解为什么会发生错误 - 由于无限循环提供“实时”数据,套接字永远不会关闭。问题是我如何检测页面更改并干净地关闭套接字?我可以在客户端关闭连接吗?如何检测页面更改呢?

这是服务器代码框架,我当然会用包含要显示的对象列表的json替换文本消息:

def event_stream():
    import time
    while True:
        time.sleep(1)
        yield "data: This is a message number X.\n\n"

@app.route('/stream')
def stream():
    return Response(event_stream(), mimetype="text/event-stream")
javascript python flask server-sent-events
4个回答
1
投票

您可以使用onBeforeUnload或jQuery的window.unload()来调用一些关闭句柄的拆卸方法。就像是:

$(window).unload(
    function() {
        $.ajax(type: 'POST',
               async: false,
               url: 'foo.com/client_teardown')
    }
}

unload() / onBeforeUnload()的处理方式存在一些不一致之处,因此您可能需要在Chrome等方面做更多的工作。


2
投票

我没有更好的答案,但我不认为上述ajax请求服务器是好的。

在flask中,SSE在Response对象中使用流,如果有一种方法可以检测Response中的断开连接或管道损坏事件,那么处理套接字事件和释放其他分配的资源会更好。


1
投票

我发现了一个脏(包括mokey修补),但工作解决方案。

因为当连接断开时exception中有SocketServer.StreamRequestHandler.finish,我们可以修补它来捕获异常并按我们的意愿处理它:

import socket
import SocketServer

def patched_finish(self):
    try:
        if not self.wfile.closed:
            self.wfile.flush()
            self.wfile.close()
    except socket.error:
        # Remove this code, if you don't need access to the Request object
        if _request_ctx_stack.top is not None:
            request = _request_ctx_stack.top.request
            # More cleanup code...
    self.rfile.close()

SocketServer.StreamRequestHandler.finish = patched_finish

如果你需要访问相应的Request对象,你还需要用flask.stream_with_context包装事件流,在我的例子中:

@app.route(url)
def method(host):
    return Response(stream_with_context(event_stream()),
                    mimetype='text/event-stream')

同样,这是一个非常脏的解决方案,如果您不使用内置的WSGI服务器,可能无法正常工作。


0
投票

不要在prod env中使用Flask内部dev wsgi服务器。考虑使用uwsgi可以优雅地处理这个套接字错误。

同时考虑使用python3也能很好地抓住这个sockrt。

© www.soinside.com 2019 - 2024. All rights reserved.