Skip to content

fix: reduce CPU spin in run_forever by increasing join timeout (closes #142)#303

Open
botbikamordehai2-sketch wants to merge 1 commit into
Lawouach:masterfrom
botbikamordehai2-sketch:fix/issue-142-1779954857
Open

fix: reduce CPU spin in run_forever by increasing join timeout (closes #142)#303
botbikamordehai2-sketch wants to merge 1 commit into
Lawouach:masterfrom
botbikamordehai2-sketch:fix/issue-142-1779954857

Conversation

@botbikamordehai2-sketch
Copy link
Copy Markdown

What

The run_forever() method in threadedclient.py uses self._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

  • Increase the join timeout from 0.1 to 5.0 seconds, reducing CPU wake-ups from 10/s to 0.2/s while still allowing SIGINT to be handled within a reasonable time.
  • Add a docstring note explaining the reason for the timeout so future developers understand the tradeoff.

Closes #142

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 from 0.1 to 5.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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants