import time
from concurrent.futures.thread import ThreadPoolExecutor
stop = False
def watch_dog():
global stop
while True:
input()
stop = not stop
tpe = ThreadPoolExecutor(1, 'watch-enter-')
tpe.submit(watch_dog)
while True:
if stop:
print('暂停中...')
time.sleep(1)
continue
print('处理任务中...')
time.sleep(1)
主线程另起一个线程监听键盘就可以了。然后通过控制一个
标识位
来控制任务是否执行。