Hi all,
I have been reading about the many ways to kill a thread in this article in python but even after reading I am not sure any of these methods would work the way I want it to. My program controls a large keg washing machine. I will have a physical push button connected to a GPIO pin for cancelling the current thread.
When the machine (thread) is running it works by turning the GPIO pins from HIGH to LOW. Also I have some while loops that wait for sensors to give the correct signal. Lots of time.sleep commands and I absolutely need it to be able to kill the thread during those commands as well as during while loops. No variables are being changed and nothing is being done besides turning on and off GPIO pins so I think I'm safe from these memory leaks I've been reading about.
I am totally ready to call this a process or whatever it takes to kill the thread immediately. Don't care what state the GPIO's are in either because I would include setting them ALL to LOW in this kill thread function.
All of my methods are basically like this:
Actually maybe the pass was the wrong way to write it those while loops, maybe I meant to write something like: if(time is reached): break
But you guys get the idea.
I have been reading about the many ways to kill a thread in this article in python but even after reading I am not sure any of these methods would work the way I want it to. My program controls a large keg washing machine. I will have a physical push button connected to a GPIO pin for cancelling the current thread.
When the machine (thread) is running it works by turning the GPIO pins from HIGH to LOW. Also I have some while loops that wait for sensors to give the correct signal. Lots of time.sleep commands and I absolutely need it to be able to kill the thread during those commands as well as during while loops. No variables are being changed and nothing is being done besides turning on and off GPIO pins so I think I'm safe from these memory leaks I've been reading about.
I am totally ready to call this a process or whatever it takes to kill the thread immediately. Don't care what state the GPIO's are in either because I would include setting them ALL to LOW in this kill thread function.
All of my methods are basically like this:
GPIO.add_event_detect(GPIO.cancelButton, GPIO.RISING, callback=cancelButtonCallback) def cancelButtonCallback(): kill_thread_func??? ### the following is an example of my thread def rinseShell(): GPIO.output(2, HIGH) GPIO.output(1, HIGH) time.sleep(20) GPIO.output(1, LOW) time.sleep(15) GPIO.output(3, HIGH) time.sleep(6) previousTime = time.time() while not liquidSensor: GPIO.output(3, HIGH) if ((time.time() - previousTime) >= 90): break time.sleep(6) GPIO.output(3, LOW) GPIO.output(2, LOW)I also have some ideas I learned from Arduinos using previousTime = time.time() and not using time.sleep() at all but I've heard that can cause the Raspberry Pi CPU to go crazy repetitively testing the while loop. But at least in this case I could use flags in the code to kill the thread (flags not shown):
def rinseShell(): GPIO.output(2, HIGH) GPIO.output(1, HIGH) previousTime = time.time() while (time.time() - previousTime >= 20): pass GPIO.output(1, LOW) previousTime = time.time() while (time.time() - previousTime >= 15): pass GPIO.output(3, HIGH) previousTime = time.time() while (time.time() - previousTime >= 6): pass previousTime = time.time() while not liquidSensor: GPIO.output(3, HIGH) if ((time.time() - previousTime) >= 90): break previousTime = time.time() while (time.time() - previousTime >= 6): pass GPIO.output(3, LOW) GPIO.output(2, LOW)
Actually maybe the pass was the wrong way to write it those while loops, maybe I meant to write something like: if(time is reached): break
But you guys get the idea.