WebSocket-for-Python icon indicating copy to clipboard operation
WebSocket-for-Python copied to clipboard

spin loop in client/threadedclient.py causes cpu use in idle processes

Open stsci-sienkiew opened this issue 11 years ago • 0 comments

In client.threadedclient, WebSocketClient has a method run_forever(). The body of the code is:

    while not self.terminated:
        self._th.join(timeout=0.1)

top was showing bunches of processes that weren't doing anything, which I eventually traced to this spin loop. Of course, my first thought was WTF? because there is no apparent need for a timeout here. I managed to trace the code through the github history and found a reference to issue #109

I suggest a comment something like this in this function:

# signals and threads are not friends in python.  Without the timeout, you can't
# interrupt this function with SIGINT.  The timeout ensures that we return to the
# main thread periodically, so a pending SIGINT can be processed.  If this causes
# unreasonable CPU usage in your application, just override this method to not
# have a timeout, and don't try to interrupt it with SIGINT.

It is not clear to me that this is a problem for most users, but it was for me. I first noticed because the fans in my computer were speeding up even though my application wasn't doing anything. (I have lots of processes on standby.)

stsci-sienkiew avatar Sep 07 '14 18:09 stsci-sienkiew