How to terminate a thread in Python without loop in run method?

前端 未结 2 1472
迷失自我
迷失自我 2021-01-22 09:43

Having class which has a long method.
Creating a thread for that method.
How i can kill\\terminate this thread?
Main problem is that i can\'t check for threa

相关标签:
2条回答
  • 2021-01-22 10:21

    While it is not a good idea to kill a thread, if you really must do it, the easiest solution is to implement a running semaphor, divide your time consuming method in sub_methods and check for thread status between the submethods.

    Code partly copied from this SO question:

    class StoppableThread(threading.Thread):
        """Thread class with a stop() method. The thread itself has to check
        regularly for the stopped() condition."""
    
        def __init__(self,la_object):
            super(StoppableThread, self).__init__()
            self.la = la_object
            self._stop = threading.Event()
    
        def stop(self):
            self._stop.set()
    
        def stopped(self):
            return self._stop.isSet()
    
        def run(self):
           self.la.time_consuming_action( self.stopped )
    
        class La :
    
          def __init__(self):
          #init here
    
          def time_consuming_action(self, thread_stop_callback ):
    
              sub_work1()
    
              if thread_stop_callback():
                 raise 'Thread Killed ! '
    
              sub_work2()
    
              if thread_stop_callback():
                 raise 'Thread Killed ! '
    
              sub_work3()
    
              #etc...
    
    0 讨论(0)
  • 2021-01-22 10:25

    This code works fine but there is a need to explicitly flush data from standard output.
    Haven't found a way where prints would work without flushing.

    import time
    from multiprocessing.process import Process
    import sys
    
    class LongAction:
        def time_consuming_action(self):
            tmax = 600
            for i in range(tmax):
                print i
                time.sleep(1)
                sys.stdout.flush()
            time.sleep(tmax)
            self.tmax = tmax
            return "Slept well"
            sys.stdout.flush()
    
    class LongActionThread(Process):
        def __init__(self, la_object):
            self.la = la_object
            Process.__init__(self)
    
        def run(self):
            self.la.time_consuming_action()
    
    if __name__ == "__main__":
        la = LongAction()
        la_thread = LongActionThread(la)
        la_thread.start()
    
        # After 5 sec i've changed my mind and trying to kill LongActionThread
        time.sleep(5)
        print "Trying to kill LongActionThread"
        la_thread.terminate()
    
    0 讨论(0)
提交回复
热议问题