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


Messages In This Thread
kill thread or process asap, even during time.sleep - by nanok66 - Apr-27-2020, 11:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 347 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  Use subprocess.Popen and time.sleep chucky831 2 2,069 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,793 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Process doesn't work but Thread work ! mr_byte31 4 2,864 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,962 Apr-13-2021, 11:44 AM
Last Post: quest
  Can you end the Time.sleep function boier96 9 10,217 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Code taking too much time to process ErPipex 11 5,278 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  Using a button to kill and restart a script duckredbeard 3 3,507 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  time.sleep mtnwinds 4 3,046 May-21-2020, 10:12 AM
Last Post: Larz60+
  how to check for thread kill flag nanok66 1 2,283 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