Thread that I can pause and resume?

后端 未结 2 997
北恋
北恋 2020-11-28 11:38

I\'m trying to create a thread, that does stuff in the background. I need to be able to effectively \'pause\' it when I need to and \'resume\' it again later. Also, if the t

相关标签:
2条回答
  • 2020-11-28 11:54

    Conditions can be used for this.

    Here's an example filling in your skeleton:

    class Me(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            #flag to pause thread
            self.paused = False
            # Explicitly using Lock over RLock since the use of self.paused
            # break reentrancy anyway, and I believe using Lock could allow
            # one thread to pause the worker, while another resumes; haven't
            # checked if Condition imposes additional limitations that would 
            # prevent that. In Python 2, use of Lock instead of RLock also
            # boosts performance.
            self.pause_cond = threading.Condition(threading.Lock())
    
        def run(self):
            while True:
                with self.pause_cond:
                    while self.paused:
                        self.pause_cond.wait()
    
                    #thread should do the thing if
                    #not paused
                    print 'do the thing'
                time.sleep(5)
    
        def pause(self):
            self.paused = True
            # If in sleep, we acquire immediately, otherwise we wait for thread
            # to release condition. In race, worker will still see self.paused
            # and begin waiting until it's set back to False
            self.pause_cond.acquire()
    
        #should just resume the thread
        def resume(self):
            self.paused = False
            # Notify so thread will wake after lock released
            self.pause_cond.notify()
            # Now release the lock
            self.pause_cond.release()
    

    Hope that helps.

    0 讨论(0)
  • 2020-11-28 11:54

    Use threading.Event instead of a boolean variable, and add another event for busy state:

    def __init__(self):
        ...
        self.can_run = threading.Event()
        self.thing_done = threading.Event()
        self.thing_done.set()
        self.can_run.set()    
    
    def run(self):
        while True:
            self.can_run.wait()
            try:
                self.thing_done.clear()
                print 'do the thing'
            finally:
                self.thing_done.set()
    
    def pause(self):
        self.can_run.clear()
        self.thing_done.wait()
    
    def resume(self):
        self.can_run.set()
    

    edit: previous answer was wrong, I fixed it and changed variable names to be clear

    0 讨论(0)
提交回复
热议问题