简体   繁体   中英

Run GPIO process in background python

I'm using Raspberry Pi 3 and DHT11 ( temp&humid sensor) to get surrounding values.

From time to time, while accessing sensor via its python module, a stall of 2-5 seconds until data is sent back back to RPI (and GUI for displaying results) occurs. This issue happens also when only printing data to terminal and not regarding GUI updating.

This delay causes a stall of entire GUI.

Regarding this stall in getting data measurement as a system limitation, I'm wondering if it is possible to run this process in parallel / background (updating a temp variable) - that it won't stall entire GUI ?

The following function access the sensor and retrieve data:

ht_data():
    h0,t0 = Adafruit_DHT.read_retry (11,4) # DHT module to obtain T&H 
    temp_var.set("%d"%t)  ## update StringVar
    hum_var.set("%d%%"%h) ## update StringVar
root.after(500, ht_data)

There might be a simpler solution.

As you can see here , read_retry simply uses time.sleep() between retries which is indeed undesirable from a GUI standpoint.

So try using read() instead of read_retry() , and do not update the values if it returns (None, None) .

ht_data():
    h0,t0 = Adafruit_DHT.read(11,4)
    if h0 and t0:
        temp_var.set("%d" % t0)  ## update StringVar
        hum_var.set("%d%%" % h0) ## update StringVar
    root.after(2000, ht_data)

Since the documentation indicates that you can only read it once every two seconds, I increased the timeout to match that.

Only if using read() doesn't work should you use a separate process (or thread).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM