本文实例讲述了Python提示[Errno 32]Broken pipe导致线程crash错误解决方法。分享给大家供大家参考。具体方法如下:
1. 错误现象
ThreadingHTTPServer 实现的 http 服务,如果客户端在服务器返回前,主动断开连接,则服务器端会报 [Errno 32] Broken pipe 错,并导致处理线程 crash.
下面先看个例子,python 版本: 2.7
示例代码
#!/usr/bin/env python #!coding=utf-8 import os import time import socket import threading from BaseHTTPServer import HTTPServer ,BaseHTTPRequestHandler from SocketServer import ThreadingMixIn class RequestHandler(BaseHTTPRequestHandler): def do_GET(self): """ 处理get请求 """ query=self.path print "query: %s thread=%s" % (query, str(threading.current_thread())) #ret_str="<html>" + self.path + "<br>" + str(self.server) + "<br>" + str(self.responses) + "</html>" ret_str="<html>" + self.path + "<br>" + str(self.server) + "</html>" time.sleep(5) try: self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(ret_str) except socket.error, e: print "socket.error : Connection broke. Aborting" + str(e) self.wfile._sock.close() # close socket self.wfile._sock=None return False print "success prod query :%s" % (query) return True #多线程处理 class ThreadingHTTPServer(ThreadingMixIn,HTTPServer): pass if __name__ == '__main__': serveraddr = ('',9001) ser = ThreadingHTTPServer(serveraddr,RequestHandler) ser.serve_forever() sys.exit(0)
[~]$curl -s 'http://10.232.41.142:9001/hello1′ <html>/hello1<br><__main__.ThreadingHTTPServer instance at 0x37483b0></html>[~]$ 此时服务器端输出日志如下: $./thread_http_server_error.py query: /hello1 thread= search041142.sqa.cm4.tbsite.net – - [15/May/2014 15:02:27] “GET /hello1 HTTP/1.1″ 200 - success prod query :/hello1
[~]$curl -s 'http://10.232.41.142:9001/hello2′ [~]$ ctrl+C
query: /hello2 thread= search041142.sqa.cm4.tbsite.net – - [15/May/2014 15:33:10] “GET /hello2 HTTP/1.1″ 200 - socket.error : Connection broke. Aborting[Errno 32] Broken pipe —————————————- Exception happened during processing of request from ('10.232.41.142′, 48769) Traceback (most recent call last): File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/SocketServer.py”, line 582, in process_request_thread self.finish_request(request, client_address) File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/SocketServer.py”, line 323, in finish_request self.RequestHandlerClass(request, client_address, self) File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/SocketServer.py”, line 639, in __init__ self.handle() File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/BaseHTTPServer.py”, line 337, in handle self.handle_one_request() File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/BaseHTTPServer.py”, line 326, in handle_one_request self.wfile.flush() #actually send the response if not already done. File “/home/wuzhu/tools/python_2_7_1/lib/python2.7/socket.py”, line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size]) AttributeError: 'NoneType' object has no attribute 'sendall'
2. 原因分析
“[Errno 32] Broken pipe “ 产生的原因还是比较明确的,由于 client 在服务器返回前主动断开连接,所以服务器在返回时写 socket 收到SIGPIPE报错。虽然在我们的程序中也对异常进行了处理,将handler 的 wfile._sock 对象close 掉 ,但python 的库里BaseHTTPServer.py中BaseHTTPRequestHandler 类的成员函数handle_one_request还是会直接调用 wfile.flush ,而没有判断 wfile 是否已经 close。
def handle_one_request(self): """Handle a single HTTP request. You normally don't need to override this method; see the class __doc__ string for information on how to handle specific HTTP commands such as GET and POST. """ try: self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.raw_requestline: self.close_connection = 1 return if not self.parse_request(): # An error code has been sent, just exit return mname = 'do_' + self.command if not hasattr(self, mname): self.send_error(501, "Unsupported method (%r)" % self.command) return method = getattr(self, mname) method() #没有判断 wfile 是否已经 close 就直接调用 flush() self.wfile.flush() #actually send the response if not already done. except socket.timeout, e: #a read or a write timed out. Discard this connection self.log_error("Request timed out: %r", e) self.close_connection = 1 return
只要在RequestHandler重载其基类BaseHTTPRequestHandler的成员函数handle_one_reques(),在调用 wfile.flush() 前加上 wfile 是否已经 close 即可。
#!/usr/bin/env python #!coding=utf-8import os import time import socket import threading from BaseHTTPServer import HTTPServer ,BaseHTTPRequestHandler from SocketServer import ThreadingMixIn
class RequestHandler(BaseHTTPRequestHandler): def handle_one_request(self): """Handle a single HTTP request. You normally don't need to override this method; see the class __doc__ string for information on how to handle specific HTTP commands such as GET and POST. """ try: self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.raw_requestline: self.close_connection = 1 return if not self.parse_request(): # An error code has been sent, just exit return mname = 'do_' + self.command if not hasattr(self, mname): self.send_error(501, "Unsupported method (%r)" % self.command) return method = getattr(self, mname) print "before call do_Get" method() #增加 debug info 及 wfile 判断是否已经 close print "after call do_Get" if not self.wfile.closed: self.wfile.flush() #actually send the response if not already done. print "after wfile.flush()" except socket.timeout, e: #a read or a write timed out. Discard this connection self.log_error("Request timed out: %r", e) self.close_connection = 1 return def do_GET(self): """ 处理get请求 """ query=self.path print "query: %s thread=%s" % (query, str(threading.current_thread())) ret_str="<html>" + self.path + "<br>" + str(self.server) + "</html>" time.sleep(5) try: self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(ret_str) except socket.error, e: print "socket.error : Connection broke. Aborting" + str(e) self.wfile._sock.close() self.wfile._sock=None return False print "success prod query :%s" % (query) return True #多线程处理 class ThreadingHTTPServer(ThreadingMixIn,HTTPServer): pass if __name__ == '__main__': serveraddr = ('',9001) ser = ThreadingHTTPServer(serveraddr,RequestHandler) ser.serve_forever() sys.exit(0)
[~]$curl -s 'http://10.232.41.142:9001/hello2' [~]$ ctrl+C
$./thread_http_server.pybefore call do_Get query: /hello2 thread=<Thread(Thread-1, started 1103210816)> search041142.sqa.cm4.tbsite.net - - [15/May/2014 15:54:09] "GET /hello2 HTTP/1.1" 200 - socket.error : Connection broke. Aborting[Errno 32] Broken pipe after call do_Get after wfile.flush()
问题内容: python 3.5的新功能之一是受此项目启发的类型提示。 键入:PEP 484 –键入提示。 我想测试它,但是它没有按预期工作。 结果是: 我期待一个错误,因为我期望布尔作为回报。我在python:3.5(docker)和local上测试了它。我是否想念一些东西以使其起作用?这种键入是否在运行时不起作用(例如python app.py)? 问题答案: 请参阅您链接到的PEP中摘要的第
本文向大家介绍Python pip安装模块提示错误解决方案,包括了Python pip安装模块提示错误解决方案的使用技巧和注意事项,需要的朋友参考一下 问题如下 python pip安装模块提示错误failed to create process 原因: 报这个错误的原因,是因为python的目录名称或位置发生改动。 解决办法: 1.找到修改python所在的目录,打开scripts目录,如下图找
本文向大家介绍JS提示:Uncaught SyntaxError:Unexpected token ) 错误的解决方法,包括了JS提示:Uncaught SyntaxError:Unexpected token ) 错误的解决方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JS提示:Uncaught SyntaxError:Unexpected token ) 错误的解决方法。分享给大
我有一个抽象的泛型“Foo”解决方案类,我的特定实现继承了它。此类是泛型的,允许实现类定义其特定的规划实体,该实体扩展了“Bar” 这会扼杀 optaplanner,这会吐出如下错误: solutionProperty(列表)未按预期克隆。FieldAccessingSolutionCloner无法识别该属性的字段,可能是因为其字段名不同。 我已经尝试用特定的实现类替换“EntityType ”,
本文向大家介绍Apache启动提示错误undefined symbol: libiconv_open解决方法,包括了Apache启动提示错误undefined symbol: libiconv_open解决方法的使用技巧和注意事项,需要的朋友参考一下 昨晚整合apache和php是实在找不到解决办法,Google 百度都转一圈了,总算在百度找回来解决方法,记录一下,这个问题是在apache启动时出
本文向大家介绍Android线程中设置控件的值提示报错的解决方法,包括了Android线程中设置控件的值提示报错的解决方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android线程中设置控件的值提示报错的解决方法。分享给大家供大家参考,具体如下: 在Android线程中设置控件的值一般会与Handler联合使用,如下: 更多关于Android相关内容感兴趣的读者可查看本站专题:《A