garyns / pigpio-dht

DHT11 Temperature and Humidity Sensor using pigpio
GNU General Public License v3.0
12 stars 6 forks source link

sleep(pause_secs) in __read() in DHTXX blocks the thread #4

Open Blindfreddy opened 4 years ago

Blindfreddy commented 4 years ago

This is not strictly a defect but I would suggest to simply return the last reading if the delay has not been reached. So acquire first reading and store it internally. Next time read() is called check if delay has been reached, as you already do, but don't sleep. Instead, if delay has not been reached return the previous reading; if it has been reached actually acquire a new reading, store it in the internal variable and return it.

Also, the delay could be a class attribute, 1s for DHT22 and 2s for DHT11 because all sensors of a given type have the same characteristic.