我需要一个程序来接受来自客户端的请求,并每5秒处理一次。这是我的代码:
def run(self):
global numConn
numConn += 1
print("Got connection number ",numConn)
#print(self.ip,self.port,self.conn)
while True :
data = self.conn.recv(1024)
hostname = abc
self.conn.send(hostname.encode())
# To process request in every 5 secs:
def printit():
global pFlag
threading.Timer(5.0, printit).start()
#print("Hello the length is",len(store_thread))
if pFlag == 0:
pFlag = 1
for h in store_thread:
store_thread.remove(h)
h.start()
pFlag = 0
printit()
while 1:
#print("Multithreaded Python server : Waiting for connections from TCP clients...")
print('This is new connection')
(conn, (ip,port)) = sock.accept()
conn_ip_port[currentConn] = (conn,ip,port)
currentConn += 1
temp = 0
newthread = ClientThread(conn_ip_port[temp][1],conn_ip_port[temp][2],conn_ip_port[temp][0])
store_thread.append(newthread)
temp += 1
threads.append(newthread)
currentConn = 0
但是在这里,一旦执行printit()函数,它就开始接受连接。我希望它应该继续接受连接并存储在列表中,而不必等待printit()函数完成。