强制结束使用计时器接受输入的函数

时间:2017-05-01 07:44:29

标签: python windows python-2.7 windows-10

我已尝试使用此处发布的解决方案:

How would I stop a while loop after n amount of time?

但该循环不会以接受输入的函数运行结束。如何强制function5退出?

    while True:
                test = 0
                if test == 5 or time.time() > timeout:
                    print "time's out!"
                    break
                test = test - 1
                function5()
   def function5():
                receiver = raw_input("enter something: ")

1 个答案:

答案 0 :(得分:0)

您已经确定了自己的问题。这不会起作用,因为当你调用raw_input()时,程序将永远等待,直到用户输入内容,你想要的是超时以某种方式中断raw_input()。

以下是* nix解决方案:

Keyboard input with timeout in Python

相关问题