Replaced busy-wait loop with threading.Event to fix 100% CPU load

This commit is contained in:
esk4nz
2026-03-23 23:28:17 +02:00
parent ad70519f47
commit e8ff1c6cbd

View File

@@ -3,6 +3,7 @@ import os
from app.adapters.agent_mqtt_adapter import AgentMQTTAdapter from app.adapters.agent_mqtt_adapter import AgentMQTTAdapter
from app.adapters.hub_http_adapter import HubHttpAdapter from app.adapters.hub_http_adapter import HubHttpAdapter
from app.adapters.hub_mqtt_adapter import HubMqttAdapter from app.adapters.hub_mqtt_adapter import HubMqttAdapter
from threading import Event
from config import ( from config import (
MQTT_BROKER_HOST, MQTT_BROKER_HOST,
MQTT_BROKER_PORT, MQTT_BROKER_PORT,
@@ -25,6 +26,9 @@ if __name__ == "__main__":
], ],
) )
# Initialize the stop event to prevent high CPU usage
stop_event = Event()
# Logic to select the adapter based on configuration (SCRUM-93 & SCRUM-94) # Logic to select the adapter based on configuration (SCRUM-93 & SCRUM-94)
# This allows easy switching between HTTP and MQTT protocols # This allows easy switching between HTTP and MQTT protocols
if HUB_CONNECTION_TYPE.lower() == "http": if HUB_CONNECTION_TYPE.lower() == "http":
@@ -55,10 +59,14 @@ if __name__ == "__main__":
agent_adapter.connect() agent_adapter.connect()
agent_adapter.start() agent_adapter.start()
# Keep the system running indefinitely to process incoming data streams logging.info("Edge module started successfully. Waiting for data...")
while True:
pass # Block the main thread efficiently without consuming CPU cycles
stop_event.wait()
except KeyboardInterrupt: except KeyboardInterrupt:
# Stop the MQTT adapter and exit gracefully if interrupted by the user # Stop the MQTT adapter and exit gracefully if interrupted by the user
logging.info("Stop signal received. Shutting down...")
agent_adapter.stop() agent_adapter.stop()
stop_event.set() # Release the event
logging.info("System stopped.") logging.info("System stopped.")