This is a thread where I am always checking if one (or more) of many occupancy sensors will return 1 (someone entered a room) in order to turn on the lights. However, it's all written in while True
loop which I think will be very bad for the memory because I'm always storing in a variable. Noting that return_message()
function is always returning a value. So how to fix this problem?
def turning_lights_on():
while True:
for occupancy_sensor_ids in ids :
# get the lights ids in the room according to the occupancy sensor id
lights_id = db.light_devices(occupancy_sensor_ids)
# the return value of the occupancy sensor is stored in message_occ_sensor
# but a topic containing the id of the sensor must be used here, so need to be changed
message_occ_sensor = occupancy_sensor.return_message(occupancy_sensor_ids)
# loop to send commands to all the lights in the room
for lights in lights_id:
# the state of lights ON/OFF is returned
# I considered that every topic starts by the device ID
state_light_tunable = light_sub.return_message(lights +"/stat/POWER")
if message_occ_sensor == "1" :
if state_light_tunable == "OFF":
# topics are to be modified and deviceID must be imported from mongoDB
#lights_id is imported from mongoDB
client.publish(lights +"/cmnd/POWER", 1)
Just because something is in a while True
loop, doesn't mean that it is necessarily going to have memory issues.
You're not holding onto any data between iterations of the loop, all of your variables get overwritten, so the old data will have no references, and the Python garbage-collector will delete them for you.
No need to worry!