Python线程异常&;插座断开/WinError 10053
我试图在套接字连接上运行心跳消息(它每30秒发送一条消息),所以我在它自己的线程上运行它,并尝试是否作为守护进程。我在一台虚拟机上--Windows x86 程序运行正常,并传输消息,但在心跳的第12次迭代中,我开始得到“线程错误!”(还有插座断开)——我试过关闭防火墙,类似于机器相关的东西,但不知道连接发生了什么/为什么断开。我也递归地尝试过,没有线程——我收到WinError 10053,主机中的软件中止了已建立的连接Python线程异常&;插座断开/WinError 10053,python,python-3.x,sockets,python-multithreading,fix-protocol,Python,Python 3.x,Sockets,Python Multithreading,Fix Protocol,我试图在套接字连接上运行心跳消息(它每30秒发送一条消息),所以我在它自己的线程上运行它,并尝试是否作为守护进程。我在一台虚拟机上--Windows x86 程序运行正常,并传输消息,但在心跳的第12次迭代中,我开始得到“线程错误!”(还有插座断开)——我试过关闭防火墙,类似于机器相关的东西,但不知道连接发生了什么/为什么断开。我也递归地尝试过,没有线程——我收到WinError 10053,主机中的软件中止了已建立的连接 def every(delay, task): next_tim
def every(delay, task):
next_time = time.time() + delay
while True:
time.sleep(max(0, next_time - time.time()))
try:
task()
except Exception:
print('Error threading!')
next_time += (time.time() - next_time) // delay * delay + delay
def test():
print("Threadtest", time.time())
类HB_Socket():
这是一个返回错误的图像
您无法测试代码,因为您还没有提供一个测试。请编辑问题中的代码,以提供我可以粘贴到文件中的最小值,而无需添加任何内容。运行代码并查看您看到的问题。添加代码以创建最小可复制示例--我认为它是可复制的,我想我忘了cnct方法。
def __init__(self,IP='127.0.0.1',port=4000,hb = 30):
self.IP = IP
self.port = port
self.HB = hb
self.socket = socket() #AF_INET,SOCK_DGRAM)
self.connected = False
def cnct(self):
s = self.socket
#s.connect((self.IP,self.port)) #MOVED THIS
try:
s.connect((self.IP,self.port))
print('Socket connected to {} on port {}'.format(self.IP,self.port))
on = self.logOn()
self.send(on)
print('LogOn message sent')
self.connected = True
return 1
except:
print('Socket creation failed: {}')
return 0
#Function being called in every()
def sendHB(self):
hbsocket = self.socket
print('PyHeartBeat client sending to IP {}'.format(self.IP,self.port))
#while 1:
hbsocket.sendto(HB_MSG,('127.0.0.1',self.port))
if HB_SIM:
sleep(self.HB)
if datetime.datetime.now().time().hour >= 16:
self.disconnect()
return
self.sendHB()
if __name__ == '__main__':
HB = HB_Socket()
HB.cnct()
HB_SIM = False
threading.Thread(target=every,args=(30,HB.sendHB)).start()
HB_SIM = True #Recursive solution.
#HB.sendHB()
#Daemon / Recursive
dmn = threading.Thread(target=HB.sendHB(), daemon=True)
dmn.start()