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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM