Trying to fix the high CPU uses#8
Conversation
Signed-off-by: Arghya Biswas <arghyabiswas05@gmail.com>
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes CPU usage in a water level monitoring system by implementing more efficient polling intervals and database change detection mechanisms.
- Reduced frequent database polling by implementing file modification time tracking
- Increased sleep intervals from 0.1s to 1s during normal operation and from 1s to 5s for distance checks
- Added distance validation to filter out erroneous sensor readings
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| config.yaml | Version bump from 1.2.2.1005 to 1.2.3.1006 |
| Scripts/Utility.py | Added distanceIsValid function to validate ultrasonic sensor readings |
| Scripts/Main.py | Implemented efficient database polling with file modification tracking and increased sleep intervals |
| Scripts/Common.py | Updated check intervals from 1s to 5s and added database check interval constant |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| # Only check for scheduled valve operations once per minute to reduce processing | ||
| if now.second == 0: # Only check at the beginning of each minute |
There was a problem hiding this comment.
This condition may cause valve operations to be missed if the loop iteration doesn't align with second == 0. Consider using a minute-based timestamp comparison instead of relying on the exact second alignment.
| # Smart sleep: shorter sleep when config updates are expected | ||
| if configUpdateAvailable: | ||
| # If we just processed a config update, check again sooner | ||
| time.sleep(0.5) # Quick follow-up check | ||
| else: | ||
| # Normal operation - longer sleep for CPU efficiency | ||
| time.sleep(1) |
There was a problem hiding this comment.
The magic numbers 0.5 and 1 for sleep intervals should be defined as named constants in Common.py for better maintainability and consistency with other timing constants.
| return False | ||
| if lastDistance > 0: | ||
| change = abs(distance - lastDistance) | ||
| if change / lastDistance > 0.1: # More than 10% change |
There was a problem hiding this comment.
The magic number 0.1 (10% threshold) should be defined as a named constant to make it configurable and improve code readability.
No description provided.