工作中需要实现如下的需求
1 在提示输入的同时,不断的在后台发出提示音。
2 当输入ok后,接受输入的,后台声音停止,如果其他值,则再次提示输入,后台的提示音继续提示。
之前没接触过多线程,在网上看了一圈,也没有发现很好的实现方法,只能自己写一个,基本满足了需求。
由于对多线程的掌握不足,可能这样的实现方式很挫,写的很乱,纯属业务学习,希望有高手可以指点一下多线程的学习。
import threading,time
import winsound,thread
cms=r"D:\INTEL.wav"
gaojing = '1'
class changegj(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
#global gaojing
#self.num = num
self.thread_stop = False
def run(self):
global gaojing
while gaojing != 'ok':
gaojing = raw_input("lai yi fa:")
print 'GAOJING:',gaojing
def stop(self):
self.thread_stop = True
class sound(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.thread_stop = False
def run(self):
while gaojing != 'ok':
time.sleep(3)
winsound.PlaySound(cms, winsound.SND_NODEFAULT)
def stop(self):
self.thread_stop = True
def test():
thread1 = changegj(0)
thread2 = sound()
thread1.start()
#time.sleep(1)
thread2.start()
#time.sleep(2)
thread1.stop()
thread2.stop()
test()