Closed hanc00l closed 5 years ago
Because Flask test server is depended on werkzeug, which is a toy server, So this bug may be caused by werkzeug. Broken pipe error cause by server.
给flask配上了多线程,能承受住中大型产品的考验吗
这个网上有很多讨论,总的来说flask是一个 toy server,不考虑高并发性能的话,应用应该不存在问题
app.py在运行中,可能会出现以下异常:
Exception happened during processing of request from ('172.16.80.1', 52437) Traceback (most recent call last): File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread self.finish_request(request, client_address) File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python2.7/SocketServer.py", line 651, in init self.finish() File "/usr/lib/python2.7/SocketServer.py", line 710, in finish self.wfile.close() File "/usr/lib/python2.7/socket.py", line 279, in close self.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
发生这个错误的原因为:服务器在接受一个请求,如果处理时间较长,当服务器还没有处理完而客户端中断了连接时(比如关闭了浏览器),由于flask server在发送数据时没有确认连接状态而直接进行flush,就会导致发生这个错误。这个异常只会影响服务端的单个请求,不会影响并行的其它请求。
感兴趣的同学可以参见stackoverflow上的两个贴子(链接见下面)。flask是一个推荐用于小型产品或开发环境的web框架,如果是具有高并发的环境,可能就不适合了,需要使用其它web框架。
根据stackoverflow上的方法,可以采用app.run(threaded=True),但并不能从根本上解决Broken pipe的异常发生。
http://stackoverflow.com/questions/12591760/flask-broken-pipe-with-requests http://stackoverflow.com/questions/31265050/how-to-make-an-exception-for-broken-pipe-errors-on-flask-when-the-client-discon