Python Forum
kill thread or process asap, even during time.sleep
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
kill thread or process asap, even during time.sleep
#1
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:

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.
Reply
#2
I think you are trying to solve the wrong problem. You shouldn't be looking for a way to stop a thread that may be sleeping, you should look for a way to write your program so it doesn't sleep. This is not only true of machine control and process control programs, but any kind of program that has to react to unpredictable inputs or events (like a user interface for example).

Machine control code should not have any waits. Machine control code should periodically read sensors and make decisions based on the sensor feedback. Maybe you will do this using events (call some function when a button is pressed), or maybe you will have a monitor with a callback (read sensor, if value above some level execute code). If you need to do something for a period of time you record a start time and periodically check if the wait period has expired. Maybe you will have a state machine. The longest your code ever waits for anything should be shorter than the fastest your code has to respond to something.
Reply
#3
Yeah I really like the idea of not having time.sleep in this code. I have hundreds of these delays in this program. But I'm really not sure how to make it periodically check for 'time_until_next_step'

time_until_next_step = 20

previousTime = time.time()
while true:
     if (time.time() - previousTime >= time_until_next_step):
     break
Is there any way to keep the processor from checking this 'if' statement thousands of times per second? The fastest I have to do anything is 1 second so if I could make a function that checked every half second then I think that would work. No clue how to implement this.
Reply
#4
Python has an event scheduler that can be used to call code periodically.

https://docs.python.org/3.8/library/sched.html
Reply
#5
Whoa game changer!!! Writing a pass function to use with it makes it work just like time.sleep! Perfect!


import time
import sched

s = sched.scheduler(time.time, time.sleep)

def delay(set_time):
    s.enter(set_time, 1, pass_func)
    s.run()

def pass_func():
    pass

print("start: ", time.time())
delay(3)
print("after 3 delay: ", time.time())
delay(7)
print("after 7 delay: ", time.time())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 235 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  Use subprocess.Popen and time.sleep chucky831 2 1,942 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 3,520 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Process doesn't work but Thread work ! mr_byte31 4 2,610 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Time.sleep: stop appending item to the list if time is early quest 0 1,870 Apr-13-2021, 11:44 AM
Last Post: quest
  Can you end the Time.sleep function boier96 9 9,421 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Code taking too much time to process ErPipex 11 4,905 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  Using a button to kill and restart a script duckredbeard 3 3,310 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  time.sleep mtnwinds 4 2,853 May-21-2020, 10:12 AM
Last Post: Larz60+
  how to check for thread kill flag nanok66 1 2,168 May-09-2020, 10:06 PM
Last Post: nanok66

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020