0

How would i loop every 5 seconds the following code :

sendSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
sendSocket.connect ( ( 'CENSORED', 1234 ) )
sendSocket.send ( 'request posit' )
data = sendSocket.recv( 100 )
chatLog.insert(END, data)

I want a client to recieve data every 5 seconds from a server, i did a while True loop (lol) and the program interface didnt even load and the server got flooded by the client.

I just want the server to update the client every 5 seconds with a variable that is stored on the server, without a user having to manually press a button.

import socket, time
from Tkinter import *

## Main Window
gui = Tk()
gui.geometry('450x350-5+40')
gui.minsize(450,350)
gui.maxsize(450,350)
userInput = StringVar()

## Main window that displays user and server input.
chatLog = Text(gui, width=60, height=15)
chatLog.pack()

## Send Packet function, main part of the script. Sends whatever the user puts in.
def sendChat():
    sendSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    sendSocket.connect ( ( 'nope.com', 1234 ) )
    sendSocket.send ( e.get() )
    data = sendSocket.recv( 100 )
    chatLog.insert(END, data)
    sendSocket.close()

while True:
    sendSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    sendSocket.connect ( ( 'nope.com', 1234 ) )
    sendSocket.send ( 'request posit' )
    data = sendSocket.recv( 100 )
    chatLog.insert(END, data)
    sendSocket.close()
    time.sleep (5.0)

## Submit text button
b = Button(gui, text="Send", command=sendChat )
b.pack()

## Text entry box
e = Entry(gui, textvariable=userInput)
e.pack()

gui.mainloop()

2 Answers2

2

See this question:

How to make tkinter repond events while waiting socket data?

You can also use after_idle

def read_the_socket():
    sendSocket.send ( 'request posit' )
    data = sendSocket.recv( 100 )
    gui.after(5000, read_the_socket)


gui.after_idle(read_the_socket)

after_idle schedules a function the be called once the GUI isn't busy anymore. You can also use .after(time, function) to delay for a specific amount of time before calling the function again.

Finally, you should really maintain your connection to the server not reconnect each time.

Community
  • 1
  • 1
Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • 1
    `after(delay_ms, callback, args...)` is probably better here, since `after_idle` will get called alot. – Jochen Ritzel May 30 '11 at 02:19
  • This is good and kind of works but after 1 time i get [Errno 10054] An existing connection was forcibly closed by the remote host – Brandon Ingham May 30 '11 at 02:26
  • @Brandon, is the server yours? The server has to be programmed to allow multiple requests for a single connection. Otherwise you need to include the whole connect/send/recv/close in the function. – Winston Ewert May 30 '11 at 04:10
1
import time

while True:
    do_stuff ()
    time.sleep (5.0)

Ideally after do_stuff up you would calculate how long you had been asleep for, then adjust your sleep time as appropriate to get back into sync.

Edit: This assumes that's all you're doing. Otherwise use the time functions to see if it's been 5 secs. time.localtime() or something. I don't remember their names, but python's libs are well documented.

Edit 2: Although that might not apply to tkinter. Really i wouldnt have posted if the full code and tkinter had been in the original message. Sorry!

  • Ive done this, the server doesnt get flooded now but the GUI still doesn't load. Its made in tkinter. Ive edited the main post to show the entire code. – Brandon Ingham May 30 '11 at 01:50
  • I don't know tkinter, sorry, can't help you with that. I can't see a problem with `while True` at all. –  May 30 '11 at 01:54
  • EDIT: although that might not apply to tkinter. Really i wouldnt have posted if the full code and tkinter had been in the original message. Sorry! –  May 30 '11 at 02:03
  • Ah thats my fault, sorry about that. I just assumed what i made it with didnt really matter and there would be a simple solution. – Brandon Ingham May 30 '11 at 02:04