I'm doing a telemetry application using Azure IoT Hub, Azure IoT SDK in Python and a raspberry pi with temperature and humidity sensors.
Humidity + Temperature sensors => Rasperry Pi => Azure IoT Hub
For my application, I send the data with different frequencies using 2 looping threads: - One loop collect the data of the temperature sensor and send it to Azure IoT Hub every 60 seconds -One loop collect the data of the humidity sensor and send it to Azure IoT Hub every 600 seconds.
I want to close properly the 2 looping threads. They currently run with no way to break them.
I'm using Python 2.7. I heard about Event from of the library "threading, Thread", but I can't find some good example of program structure to apply.
How can I use Event to close properly thread? How to end those loops with another method?
Here is the structure of my code using the 2 threads including loop.
from threading import Thread
def send_to_azure_temperature_thread_func:
client = iothub_client_init()
while True:
collect_temperature_data()
send_temperature_data(client)
time.sleep(60)
def send_to_humidity_thread_func():
client = iothub_client_init()
while True:
collect_humidity_data()
send_humidity_data(client)
time.sleep(600)
if __name__ == '__main__':
print("Threads...")
temperature_thread = Thread(target=send_to_azure_temperature_thread_func)
temperature_thread.daemon = True
print("Thread1 init")
humidity_thread = Thread(target=send_to_azure_humidity_thread_func)
humidity_thread.daemon = True
print("Thread2 init")
temperature_thread.start()
humidity_thread.start()
print("Threads start")
temperature_thread.join()
humidity_thread.join()
print("Threads wait")