为无法停止threading的坑做一次记录
在用PyQt5界面库写socket局域网通信时,发现窗口关闭后程序仍在运行,认真检查后是线程中的socket的recv函数出了问题,在socket关闭后(socket.close()),socket.recv()仍会堵塞,导致线程无法释放,现找到了三种做法
修改前服务器部分代码如下
threading.Thread(target=self.connect).start()
def connect(self, server=None):
# 等待用户连接
if not server:
server = self.server # socket
# 服务器要一直运行所以使用死循环
while True:
# 接受所连接的客户端的信息
try:
connect, addrss = server.accept()
except OSError:
break # 服务器关闭
# 每连接一个客户端就开启一个线程
t = threading.Thread(target=self.member_receive, args=(connect, addrss))
t.start()
self.receive_thread.append(t)
def member_receive(self, connect, addrss):
while True:
try:
rData = connect.recv(1024)
except Exception as error:
print(error)
break
?然后member_receive中的connect.recv(1024)会一直堵塞运行
修改方法来自?如何从Python中的另一个线程中止socket.recv()
使用超时的解决方案。这将中断recv(实际上在超时之前),这很好:
链接中的代码:
# Echo server program
import socket
from threading import Thread
import time
class ClientThread(Thread):
def __init__(self, clientSocke):
Thread.__init__(self)
self.clientSocket = clientSocket
def run(self):
while 1:
try:
data = self.clientSocket.recv(1024)
print "Got data: ", data
self.clientSocket.send(data)
except socket.timeout:
# If it was a timeout, we want to continue with recv
continue
except:
break
self.clientSocket.close()
HOST = ''
PORT = 6000
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSocket.bind((HOST, PORT))
serverSocket.listen(1)
clientSocket, addr = serverSocket.accept()
clientSocket.settimeout(5)
print 'Got a new connection from: ', addr
clientThread = ClientThread(clientSocket)
clientThread.start()
# Close it down immediatly
clientSocket.close()
先设置超时的时间为5秒
socket.settimeout(5)
即1秒内如果没有接收到就会报出socket.timeout,然后跳到下一次循环,从而避免程序堵塞、线程无法释放
修改后服务器代码如下
self.server.settimeout(5)
threading.Thread(target=self.connect).start()
def connect(self, server=None):
# 等待用户连接
if not server:
server = self.server # socket
# 服务器要一直运行所以使用死循环
while True:
# 接受所连接的客户端的信息
try:
connect, addrss = server.accept()
except OSError:
break # 服务器关闭
# 每连接一个客户端就开启一个线程
t = threading.Thread(target=self.member_receive, args=(connect, addrss))
t.start()
self.receive_thread.append(t)
def member_receive(self, connect, addrss):
while True:
try:
rData = connect.recv(1024)
except socket.timeout:
break
except Exception as error:
print(error)
break
当然,你得确保你的超时时间足够使recv函数接收完所有的数据才行。
|