栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以暂停和继续的线程?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以暂停和继续的线程?

Condition
可以用于。

这是填写您的骨骼的示例:

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

希望能有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617484.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号