Python Forum
how to check for thread kill flag
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to check for thread kill flag
#1
Hi,

I am looking for a way to kill a thread immediately and I have some working code that accepts keyboard input (the letter Q) to kill (by setting flag _running to False) but the code is not killed until my entire for loop is completed. How could I kill the thread quicker?

import time
import sched
import threading
import keyboard

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


class Task:

    def __init__(self):
        self._running = True

    def terminate(self):
        self._running = False

    def run(self, cycle):
        while self._running:
            time.sleep(.01)
            for func in cycle:
                func()
               
    def pass_func(self):
        pass

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

    def emptyKeg(self): 
        #while self._running:         
              #I also tried using my kill flag (_running) here, as shown in line above
              #but if canceled during function, function still prints last value, this is not acceptable
        print("A start: ", time.time())
        self.delay(5)
        print("A after 5: ", time.time())

    def emptyGlass(self):
        print("B start: ", time.time())
        self.delay(5)
        print("B after 5: ", time.time())

    def printCallback(self):
        print("key was pressed")
        c.terminate()

c = Task()
cycleRun = [c.emptyKeg, c.emptyGlass]

keyboard.add_hotkey('q', callback=c.printCallback)

t = threading.Thread(target=c.run, args=[cycleRun])
t.start()


while True:
    pass


My output is below. As you can see I press a key but the thread continues to print the last value even though I canceled it.

C:\Users\Darwin\mu_code\venv\Scripts\python.exe C:/Users/Darwin/mu_code/sampleSubprocess2.py
A start:  1589050525.946932
A after 5:  1589050530.9775162
B start:  1589050530.9894502
key was pressed               #key is pressed here but thread is not killed, the next line still prints
B after 5:  1589050535.9960656
My attempt at killing the thread more quickly is not going well. I added this _running flag deeper into my "emptyKeg" and "emptyGlass" functions that do the work but if the function has started and I press cancel it will still print the last line of code in the function. I'm looking to cease execution of the thread at the immediate line of code when cancel is pressed.

How can I implement checking for the flag between every line of code? (without enclosing EVERY LINE OF CODE in a while loop, which seems stupid and messy)
Reply
#2
I think I will just add hundreds of if statements to my code. Problem solved.

I'm not sure how to delete this post so I will Set Solved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 600 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 1,347 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Using a button to kill and restart a script duckredbeard 3 2,011 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  kill thread or process asap, even during time.sleep nanok66 4 1,977 Apr-29-2020, 10:13 AM
Last Post: nanok66
  Check for a special characters in a column and flag it ayomayam 0 1,416 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 11,158 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Using a flag error blackjesus24 1 1,124 Jan-30-2020, 09:42 AM
Last Post: buran
  Identifying string success flag graham23s 4 2,190 Aug-14-2019, 09:27 PM
Last Post: graham23s
  Practicing using a "flag": please point in right direction magsloo 5 2,202 May-10-2019, 04:58 AM
Last Post: perfringo
  Linux: Automatically kill process with fixed exe path anddontyoucomebacknomore 4 2,203 Apr-22-2019, 07:35 AM
Last Post: anddontyoucomebacknomore

Forum Jump:

User Panel Messages

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