python threading.Thread暂停、唤醒、退出 不消耗cpu
class MyThreadSound(threading.Thread):
def __init__(self):
super(MyThreadSound, self).__init__()
self.isexit = False
self.ispause = True
self.pausetimeout = None # 暂停等待最大超时60S self.pausetimeout =None 表示无限等待
self.stopevent = threading.Event()
"""
暂停
"""
def pause(self):
self.ispause = True
"""
退出
"""
def exit(self):
self.isexit = True
self.wake()
"""
唤醒
"""
def wake(self):
self.ispause = False
self.stopevent.set()
def run(self):
while not self.isexit:
"""
在此做逻辑处理
"""
time.sleep(0.5)
if self.ispause:
self.stopevent.clear()
self.stopevent.wait(self.pausetimeout)
本文来自博客园,作者:{archer},转载请注明原文链接:https://www.cnblogs.com/archer-mowei/p/15710861.html