0

I'm writing a library that will connect to sockets and manage them, process its data, and do stuff based on that.

My problem lays on sending b"\r\n\x00" to the socket every 20 seconds. I thought that if I started a new thread for the ping function, that would work.

..however, time.sleep() seems to pause the whole entire program instead of what I thought would be just that thread.

here's my code thus far:

def main(self):
  recvbuf = b""
  self.connect(self.group, self.user, self.password)
  while self.connected:
    rSocket, wSocket, error = select.select([x[self.group] for x in self.conArray], [x[self.group] for x in self.conArray], [x[self.group] for x in self.conArray], 0.2) #getting already made socket connections
    for rChSocket in rSocket:
      while not recvbuf.endswith(b"\x00"): #[-1] doesnt work on empty things... and recvbuf is empty.
        recvbuf += rChSocket.recv(1024) #need the WHOLE message ;D
      if len(recvbuf) > 0:
        dataManager.manage(self, self.group, recvbuf)
        recvbuf = b""
    for wChSocket in wSocket:
      t = threading.Thread(self.pingTimer(wChSocket)) #here's what I need to be ran every 20 seconds.
      t.start()
  x[self.group] for x in self.conArray.close()

and here's the pingTimer function:

def pingTimer(self, wChSocket):
  time.sleep(20)
  print(time.strftime("%I:%M:%S %p]")+"Ping test!") #I don't want to mini-DDoS, testing first.
  #wChSocket.send(b"\r\n\x00")

Thanks :D

4

1 回答 1

1

This:

t = threading.Thread(self.pingTimer(wChSocket))

Does not do what you expect. It calls self.pingTimer in the same thread and passes the return value to threading.Thread. That's not what you want. You probably want this:

t = threading.Thread(target=self.pingTimer, args=(wChSocket,))
于 2013-04-12T02:50:01.517 回答