简体   繁体   English

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

[英]forcing to end a function that accepts input using a timer

I've tried using the the solution posted here: 我试过使用这里发布的解决方案:

How would I stop a while loop after n amount of time? 如何在经过一段时间后停止while循环?

but that loop doesn't end with the function im running that accepts inputs. 但该循环不会以接受输入的函数运行结束。 How do I force function5 to exit? 如何强制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: ")

You've already identified your problem. 你已经确定了你的问题。 This won't work because when you call raw_input() the program will wait forever until the user types something, and what you want is for the timeout to somehow interrupt raw_input(). 这不起作用,因为当你调用raw_input()时,程序将永远等待,直到用户键入内容,你想要的是超时以某种方式中断raw_input()。

Here is a *nix-only solution: 这是一个* nix唯一的解决方案:

Keyboard input with timeout in Python 键盘输入在Python中超时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM