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)
posted @ 2021-12-20 14:45  默*为  阅读(1338)  评论(0编辑  收藏  举报