fix: reduce CPU spin in run_forever by increasing join timeout (closes #142)#303
Open
botbikamordehai2-sketch wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR reduces idle CPU wakeups in the threaded WebSocket client by increasing the run_forever() join timeout while documenting the signal-handling tradeoff.
Changes:
- Increased
WebSocketClient.run_forever()join timeout from0.1to5.0. - Added docstring guidance explaining why the timeout exists and its CPU vs SIGINT responsiveness tradeoff.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| """ | ||
| while not self.terminated: | ||
| self._th.join(timeout=0.1) | ||
| self._th.join(timeout=5.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The
run_forever()method inthreadedclient.pyusesself._th.join(timeout=0.1)in a loop. This causes the main thread to wake up 10 times per second even when the WebSocket is idle, leading to noticeable CPU usage and fan spin-up in applications with many standby connections.The short timeout was originally introduced (see #109) to allow SIGINT (KeyboardInterrupt) to interrupt the blocking join call, since Python signal handlers only run in the main thread between bytecode instructions.
Fix
0.1to5.0seconds, reducing CPU wake-ups from 10/s to 0.2/s while still allowing SIGINT to be handled within a reasonable time.Closes #142