【问题标题】:Killing an input in Python在 Python 中杀死一个输入
【发布时间】:2017-01-28 17:15:11
【问题描述】:

有没有办法简单地从另一个线程中杀死 input()? 例如,如果一段时间过去了,用户仍然没有在input() 中输入任何内容,有没有办法简单地“杀死”input()

【问题讨论】:

    标签: python multithreading time


    【解决方案1】:

    你不能杀死input()。 但是,您可以使用 sys.stdin 可迭代、for 循环和标志(或某些函数)来执行几乎相同的操作。

    import sys
    
    stopReadingInput = False
    for line in sys.stdin :
        # Insert some code that involves user input
        if stopReadingInput : # Here you can call a function to check if you don't need user input anymore, instead of if statement. Otherwise you'll have to change the flag value from outside.
            break
    

    【讨论】:

    • 你能给出一个使用这种解释的完整代码示例吗?谢谢!
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    相关资源
    最近更新 更多