Author Topic: Dedicated thread sleeping?  (Read 2060 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Dedicated thread sleeping?
« on: April 11, 2013, 09:30:41 pm »
Lol, sorry if I seem to be "overly-asking" with my mini-wall of threads ive been making, but ive beein doing quite a bit of programming recently :p

Anywho, 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 D:

here's my code thus far:

Code: [Select]
  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()
    chSocket.close()

and here's the pingTimer function:

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

Thanks :D
Loved this place, still the best producers of power metal, and sparked my dreams of coding.