thread stop python

Solutions on MaxInterview for thread stop python by the best coders in the world

showing results for - "thread stop python"
Kamil
27 Jan 2019
1# Python program using
2# traces to kill threads
3  
4import sys
5import trace
6import threading
7import time
8class thread_with_trace(threading.Thread):
9  def __init__(self, *args, **keywords):
10    threading.Thread.__init__(self, *args, **keywords)
11    self.killed = False
12  
13  def start(self):
14    self.__run_backup = self.run
15    self.run = self.__run      
16    threading.Thread.start(self)
17  
18  def __run(self):
19    sys.settrace(self.globaltrace)
20    self.__run_backup()
21    self.run = self.__run_backup
22  
23  def globaltrace(self, frame, event, arg):
24    if event == 'call':
25      return self.localtrace
26    else:
27      return None
28  
29  def localtrace(self, frame, event, arg):
30    if self.killed:
31      if event == 'line':
32        raise SystemExit()
33    return self.localtrace
34  
35  def kill(self):
36    self.killed = True
37  
38def func():
39  while True:
40    print('thread running')
41  
42t1 = thread_with_trace(target = func)
43t1.start()
44time.sleep(2)
45t1.kill()
46t1.join()
47if not t1.isAlive():
48  print('thread killed')
49  
50In this code, start() is slightly modified to set the system trace function using settrace(). 
51The local trace function is defined such that,
52whenever the kill flag (killed) of the respective thread is set, 
53a SystemExit exception is raised upon the excution of the next line of code,
54which end the execution of the target function func.
55Now the thread can be killed with join().
queries leading to this page
do you have to tell a thread to stop pythonpython stop all threadshow to close thread automatically in pythonstop threading thread pythonpython stop all threads in modulestop a thread pythonpython stop thread immediately stop python threadpython thread start and stopis there a stop method in threading pythonpython how to start and stop a threadpython threading stopstop threading pythonpython threading stop thrpython threading how to stop a threadstop a running thread in pythonpython stop threading functionpython threading stop all threadsthread stop function pyqtpython how to stop a program from a threadpython stop threadingthread thread stop 28 29 pythonpython stop threadpython threading stop threadpython make sure threads closedo python threads stopwhat happens to python thread after executionthreading thread kill pythonpython kill threading threadpython how to stop a threadstop threads pythonstop python and threadspython force thread to stoppython threading stop a thread manuallyhow to stop thread execution in pythonhow to kill threading thread in pythondo python threads stop themselvesthread python stophow to close thread in pythonhow to stop thread pythonpython stop a threadstop thread in pythonpython thread stoppython stop till for threadpython how to stop threadthread exit pythonhow to stop thread in pythonthread kill pythonhow to stop python threadpython thread start stoppython stop a threadfpython suspend threadkill thread python threadingpython threading stop deamonhow we can stop a thread from threading library pythonself closing threads pythonclose thread pythonpython thread timer terminatethreading stop loop pythonthreading python closethread stop pythonpython stop for threadpython how to stop a running threadthreading python stop functionstop to stop thread in pythonthread stop pythonthreading thread python killhow do i stop python program from hanging threaddoes a python thread stop once the code in it is donepython close threadhow to stop threading timer in pythonhow to thread start stop in pythonpython thread stoppython stop thread loopthread terminate pythonpython stop threading threadpython thread stoppython stop threadsfully stop thread pythonpython threading timer stopstop threads in pythonhow to stop a daemon thread in pythonhow to stop the execution of a thread in pythonpython threading stop currentely running threadpython interrupt threadstop running thread python after timehow to stop a running thread in pythonstop a timer thread pythonthreading stop thread pythonpython threading stop current threadstop thread pythonstop program from thread pythonpython create and stop threadpython on thread close functionpython threading tread stoppython3 stop threadpython how to close a threadhow to stop a thread in pythonhow to close threads in pythonget thread stop event pythonpython how to stop all running threadspython threading thread on close functionhow to stop a thread pythonstop a thread in pythonstop a thread threading thread pythonthreading python stoppython how to stop all threadshow to stop all threads in pythonthread stop python