如何在python中处理管道破损(SIGPIPE)? [英] How to handle a broken pipe (SIGPIPE) in python?
问题描述
我用python编写了一个简单的多线程游戏服务器,该服务器为每个客户端连接创建了一个新线程。我发现每时每刻,服务器都会由于管道中断/ SIGPIPE错误而崩溃。我很确定,当程序尝试将响应发送回不再存在的客户端时,就会发生这种情况。
I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no longer present.
有什么好的方法来处理此问题?我首选的解决方案只是关闭与客户端的服务器端连接并继续,而不是退出整个程序。
What is a good way to deal with this? My preferred resolution would simply close the server-side connection to the client and move on, rather than exit the entire program.
PS:此问题/解答以通用方式解决了该问题;
PS: This question/answer deals with the problem in a generic way; how specifically should I solve it?
推荐答案
阅读try:语句。
try:
# do something
except socket.error, e:
# A socket error
except IOError, e:
if e.errno == errno.EPIPE:
# EPIPE error
else:
# Other error
这篇关于如何在python中处理管道破损(SIGPIPE)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!