-2

In this code

kill = False

a = tr.Thread(target=func, args=(a, b, kill), daemon=True)

a.start()

And this is a tkinter app so how do I kill this thread like what command is threr to do that.

Deni 'Z Off
  • 11
  • 1
  • 3
  • set `kill` to `True` and join the thread. Anyway, since your thread is a Daemon, it will stop when all other non-Daemon threads and the main stop – Gsk Aug 05 '20 at 11:58

1 Answers1

4

There are different methods to kill a thread

  • In the above code, as soon as the global variable stop_threads is set, the target function run() ends and the thread t1 can be killed by using t1.join(). But one may refrain from using global variable due to certain reasons. For those situations, function objects can be passed to provide a similar functionality as shown below.
# Python program killing 
# threads using stop 
# flag 
import threading 
import time 
def run(stop): 
    while True: 
        print('thread running') 
        if stop(): 
                break
def main(): 
        stop_threads = False
        t1 = threading.Thread(target = run, args =(lambda : stop_threads, )) 
        t1.start() 
        time.sleep(1) 
        stop_threads = True
        t1.join() 
        print('thread killed') 
main() 

The function object passed in the above code always returns the value of the local variable stop_threads. This value is checked in the function run(), and as soon as stop_threads is reset, the run() function ends and the thread can be killed.

  • Using traces to kill threads : This methods works by installing traces in each thread. Each trace terminates itself on the detection of some stimulus or flag, thus instantly killing the associated thread. For Example
# Python program using 
# traces to kill threads 

import sys 
import trace 
import threading 
import time 
class thread_with_trace(threading.Thread): 
  def __init__(self, *args, **keywords): 
    threading.Thread.__init__(self, *args, **keywords) 
    self.killed = False
  def start(self): 
    self.__run_backup = self.run 
    self.run = self.__run       
    threading.Thread.start(self) 

  def __run(self): 
    sys.settrace(self.globaltrace) 
    self.__run_backup() 
    self.run = self.__run_backup 
  def globaltrace(self, frame, event, arg): 
    if event == 'call': 
      return self.localtrace 
    else: 
      return None

  def localtrace(self, frame, event, arg): 
    if self.killed: 
      if event == 'line': 
        raise SystemExit() 
    return self.localtrace 
  def kill(self): 
    self.killed = True
def func(): 
  while True: 
    print('thread running') 
t1 = thread_with_trace(target = func) 
t1.start() 

time.sleep(2) 
t1.kill() 
t1.join() 

if not t1.isAlive(): 

  print('thread killed') 

In this code, start() is slightly modified to set the system trace function using settrace(). The local trace function is defined such that, whenever the kill flag (killed) of the respective thread is set, a SystemExit exception is raised upon the excution of the next line of code, which end the execution of the target function func. Now the thread can be killed with join().

Ujjwal Dash
  • 767
  • 1
  • 4
  • 8