On a Raspberry Pi I'm running two processes which loop constantly until an input is triggered which stops one of the processes but how do I stop both processes? The only thing that works is pressing control+c in the console I can't get it to stop with code at this moment.
def stopButton():
while 1:
wiringpi2.pinMode(52,0)
stopBut = wiringpi2.digitalRead(52)
print (stopBut)
wiringpi2.delay(500)
if (stopBut == 0):
stopBlink()
def stopBlink():
redBlink = int(0)
while (redBlink < int(20)):
wiringpi2.digitalWrite(pin2,HIGH)
wiringpi2.delay(50)
wiringpi2.digitalWrite(pin2,LOW)
wiringpi2.delay(50)
redBlink += int(1)
The above code simply looks for a button press, once the button is pressed it triggers to stopBlink function which flashes a red LED 20times.
def testRunning():
while 1:
wiringpi2.digitalWrite(pin3,HIGH) # Write HIGH to pin 2(U14 pin 2)
wiringpi2.delay(1000)
wiringpi2.digitalWrite(pin3,LOW) # Write LOW to pin
wiringpi2.delay(1000)
The above code simply flashes a blue LED on and off in a loop.
if __name__ == '__main__':
try:
P1 = Process(target = stopButton)
P2 = Process(target = testRunning)
P1.start()
P2.start()
P1.join()
P2.join()
Now when I hit the stopBlink function I want it to stop all other running processes but I just can't seem to get it to stop the testRunning function. I've tried adding
sys.exit(1)
To the stopBlink function but it has no affect on the other functions running.