Python Forum
how to check for thread kill flag - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to check for thread kill flag (/thread-26687.html)



how to check for thread kill flag - nanok66 - May-09-2020

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)


RE: how to check for thread kill flag - nanok66 - May-09-2020

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.