简体   繁体   English

我可以暂停和恢复的线程?

[英]Thread that I can pause and resume?

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 thread is in the middle of doing something when I 'pause' it, it should make the calling thread wait until it finishes what it's doing. 此外,如果线程在我“暂停”它时正在做某事,它应该让调用线程等到它完成它正在做的事情。

I'm pretty new to Multithreading in Python, so I haven't gotten all that far. 我对Python中的多线程很新,所以我还没有那么远。

What I have pretty much does everything except make the calling thread wait if pause is called while my thread is doing something. 除了让调用线程等待,如果在我的线程正在执行某些操作时调用暂停,我几乎可以做的事情。

Here's the outline of what I'm trying to achieve in code: 以下是我在代码中尝试实现的概述:

import threading, time

class Me(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        #flag to pause thread
        self.paused = False

    def run(self):
        while True:
            if not self.paused:
                #thread should do the thing if
                #not paused
                print 'do the thing'
                time.sleep(5)

    def pause(self):
        self.paused = True
        #this is should make the calling thread wait if pause() is
        #called while the thread is 'doing the thing', until it is
        #finished 'doing the thing'

    #should just resume the thread
    def resume(self):
        self.paused = False

I think I basically need a locking mechanism, but within the same thread? 我想我基本上需要一个锁定机制,但在同一个线程内?

Condition s can be used for this . Condition s可用于此

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. 希望有所帮助。

Use threading.Event instead of a boolean variable, and add another event for busy state: 使用threading.Event而不是布尔变量,并为busy状态添加另一个事件:

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 编辑 :上一个答案是错误的,我修复了它并更改了变量名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM