在Python中停止输入

时间:2017-06-13 17:10:41

标签: python python-3.x input timer python-multithreading

我尝试停止输入功能,但它没有用完。它可以在4个输入后停止定时器,但定时器不会停止输入功能。我正在使用Windows,所以我不能使用Signal。

from threading import Thread
from threading import Timer
import sys
x=3
def killThread ():
    print ("timeout")
    return
    sys.exit(0)
def function ():
    global x
    summa = 0
    if t.isAlive:
        while x>=0:
            y = int(input ("Number: "))
            x=x-1
            summa = summa + y
            print (summa)
        print ("it's enough")
        t.cancel ()
        sys.exit(0)

if __name__ == '__main__':

t1 = Thread (target=function, daemon=True)
t1.setDaemon (True)
t = Timer(5.0, killThread)
t.start()
t1.start ()

0 个答案:

没有答案