I wrote a chat program and have a GUI running using Tkinter, and to go and check when new messages have arrived, I create a new thread so Tkinter keeps doing its thing without locking up while the new thread goes and grabs what I need and updates the Tkinter window. This however becomes a huge CPU hog, and my guess is that it has to do somehow with the fact that the Thread is started and never really released when the function is done.
Here's the relevant code (it's ugly and not optimized at the moment, but it gets the job done, and itself does not use too much processing power, as when I run it not threaded, it doesn't take up much CPU but it locks up Tkinter)
Note: This is inside of a class, hence the extra tab.
def interim(self):
threading.Thread(target=self.readLog).start()
self.after(5000,self.interim)
def readLog(self):
print 'reading'
try:
length = len(str(self.readNumber))
f = open('chatlog'+str(myport),'r')
temp = f.readline().replace('\n','')
while (temp[:length] != str(self.readNumber)) or temp[0] == '<':
temp = f.readline().replace('\n','')
while temp:
if temp[0] != '<':
self.updateChat(temp[length:])
self.readNumber +=1
else:
self.updateChat(temp)
temp = f.readline().replace('\n','')
f.close()
Is there a way to better manage the threading so I don't consume 100% of the CPU very quickly?