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