Skip to content

Commit 3bdec5c

Browse files
authored
fix(device): prevent while true loop high cpu usage bug (#298)
add a sleep in while true loop and prevent high cpu usage bug <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved performance by adding a delay in the processing loop to reduce CPU resource consumption. - Enhanced logging detail for socket error messages to aid in debugging. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 7b4bf8f commit 3bdec5c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Diff for: midealocal/device.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def _check_heartbeat(self, now: float) -> None:
637637

638638
def _recovery_timeout(self) -> None:
639639
if not self._socket:
640-
_LOGGER.warning("[%s] _recovery_timeout socket error", self._device_id)
640+
_LOGGER.debug("[%s] _recovery_timeout socket error", self._device_id)
641641
raise SocketException
642642
try:
643643
self._socket.settimeout(SOCKET_TIMEOUT)
@@ -740,6 +740,8 @@ def run(self) -> None:
740740
)
741741
self.close_socket()
742742
break
743+
# prevent while True loop cpu 100%
744+
time.sleep(0.1)
743745

744746
def set_attribute(self, attr: str, value: bool | int | str) -> None:
745747
"""Set attribute."""

0 commit comments

Comments
 (0)