我可以暂停和继续的线程? [英] Thread that I can pause and resume?

查看:55
本文介绍了我可以暂停和继续的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个线程,该线程在后台执行操作.我需要能够在需要时有效地暂停"并稍后再次恢复".另外,如果我暂停"该线程时正在执行某项操作,则该线程应使调用线程等到完成其操作为止.

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.

我对Python中的多线程技术还很陌生,所以我还没走那么远.

I'm pretty new to Multithreading in Python, so I haven't gotten all that far.

除了线程正在做某事时调用pause之外,使调用线程等待的一切几乎都是我所能做的.

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可以用于此.

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()

希望有帮助.

这篇关于我可以暂停和继续的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆