嗨为什么我的KeyboardInterrupt:当我点击控制c或控制x时,我的程序没有停止?这是我目前的代码。
我正在使用运行2函数coinPulser和coinPulserDone的python Threading。
import threading
import time
lock = threading.Lock()
counter = 0
input = 3
def coinPulser ():
global counter
global input
lock.acquire()
try:
while counter < input:
counter+=1
time.sleep(.1)
if counter in [1,3,5]:
print(counter)
return counter
finally:
lock.release()
def coinPulserDone ():
while True:
print(coinPulser())
try:
coinpulser = threading.Thread(target = coinPulser)
coinpulser.start()
coinpulserdone = threading.Thread(target = coinPulserDone)
coinpulserdone.start()
except KeyboardInterrupt:
coinpulser.stop()
coinpulserdone.stop()
print('Thread Stops')
我怀疑问题是你的代码在按Cntr-C之前退出try / except块。您需要添加某种形式的循环,将其保存在该块中。一个简单的循环,如
while True:
time.sleep(1)
在你的除外线之前应该做的伎俩。