threading in Python taking up too much CPU threading in Python taking up too much CPU tkinter tkinter

threading in Python taking up too much CPU


It looks like you are creating a new thread every five seconds. If the function you're calling takes more than five seconds, these threads are going to start stacking up on you. Perhaps one solution is to not spawn a new thread every five seconds, but wait for the first thread to finish, and then wait fives seconds to spawn another thread.

There really isn't a reason to keep reading the same file over and over and over, is there? Why not read it all once, and keep what you read in memory. Then, when you read it again in five seconds, you can skip over all the bytes you've already read (via seek()), and just read the new data that was added. With that, you don't even need to use threads.

It seems you're doing way more work than you need to, when it looks like all you're really trying to do is emulate 'tail -f'.


It's difficult to find a supposed performance/threading problem without some code that one can run.

Are you sure that it's threading that sucks up all the cpu? Seems pretty strange to me. If you substitute

threading.Thread(target=self.readLog).start()

with

self.readLog()

does it use less CPU?

In case you check for new messages really often, the creation of the thread could be a problem, i suggest you to use only one thread with a loop to check for new messages with some wait/sleep queue/signal based way to trigger a new loop.