no0by5 no0by5 - 1 year ago 82
Python Question

Calling a Method several times with several Threads

I want a LED to flash, while some work is beeing done on my Raspberry. I am using a Thread for the LED in my Python script.

The initial code:

import RPi.GPIO
import time
import threading

pinLED = 10
pinButton = 12

GPIO.setup(pinLED, GPIO.OUT)
GPIO.setup(pinButton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.output(pinLED, 0)

Method for the Thread:

working = False
def flash():
status = 0
while working:
if status == 0:
GPIO.output(pinLED, 1)
status = 1
GPIO.output(pinLED, 0)
status = 0|
GPIO.output(pinLED, 0)


while True:
time.sleep(0.02) #found out that 0.02 is a good value to get every tiny button push and save resources

if GPIO.input(pinButton) == 1:
t = threading.Thread(target=flash)
working = True

time.sleep(5) #work would be here
working = False

except Exception as e:

When I start the script and press the button the first time, everything is working and the led is flashing. But when i press the button a second time, without restarting the script, the led isn't flashing. I printed some debug messages and found out, that t.start() is called, but for some reason it's doing nothing, also no Exception is thrown. Shouln't the LED start flashing every time I press the button again?

Answer Source

I found no logic fault and i confirm it works, but used the following changes:

  1. Starting the main_thread from inside if __name__ == '__main__':.
    I suggest to move also all GPIO calls inside this block.

Avoid placing code that is executed on startup outside of if __name__ == '__main__':

From the docs: Safe importing of main module One should protect the “entry point” of the program by using if __name__ == '__main__':

  1. Added join() after working = False, this guaranteed the thread has terminated befor starting it again.

    working = False  

I would suggest to change the def flash(), to the following:
Using threading.Event() instead of a global Instance and passing it together with the pinLED. This generalizes the def flash(...) and allow its use with different pinLED, even parallel. Define status as threading.local() threadsafe, so instance’s values will be different for separate threads.
For instance:

def flash(pinLED, flashing):
    status = threading.local()
    status.LED = False
    while flashing.is_set():
        status.LED = not status.LED
        GPIO.output(pinLED, int(status.LED))

    GPIO.output(pinLED, 0)

Changes to the main_thread:

def main_thread():
    flashing = threading.Event()

        while True:
            if GPIO.input(pinButton) == 1:
                t = threading.Thread(target=flash, args=(pinLED, flashing,))

                time.sleep(2)  # work would be here


Tested with Python:3.4.2

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download