收集数据后中断time.sleep()

时间:2017-10-06 18:59:59

标签: python multithreading

这是我的代码:

from threading import Thread
from time import sleep
from msvcrt import getch

key = 0

def ProcessKey(key):
    if key == 97:
        return 1
    elif key == 32:
        return 2
    elif key == 13:
        return 3
    else:
        return 0

def Check():
    sleep(2)
    if key != 0:
        return

Thread(target = Check()).start()
key = ord(getch())
print(ProcessKey(key))

定时输入在切断某人输入输入时工作正常,但如果有人及时输入输入,我必须在执行下一行代码之前等待整整2秒。在有人输入输入后,如何跳过剩下的2秒钟?

1 个答案:

答案 0 :(得分:0)

from threading import Thread
from time import sleep
from msvcrt import getch

key = 0

def ProcessKey(key):
    if key == 97:
        return 1
    elif key == 32:
        return 2
    elif key == 13:
        return 3
    else:
        return 0

def Check():
    sleep(2)
    if key != 0:
        return

Thread(target = Check).start() ##Dont call the function just enter the name I think this will solve
key = ord(getch())
print(ProcessKey(key))