首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python用户输入问题

Python用户输入问题
EN

Stack Overflow用户
提问于 2016-01-26 16:02:54
回答 1查看 2.3K关注 0票数 0

问题

在执行这两个函数时与STDIN发生冲突。什么会导致第二个函数不能正确读取STDIN?如果没有执行第一个函数,则第二个函数在读取输入时没有问题。

要清除STDIN缓冲区,我尝试过:

代码语言:javascript
复制
'sys.stdin.flush'

'tcflush(sys.stdin, TCIOFLUSH)'

Python代码

代码语言:javascript
复制
import sys, select

def stdin_read():

    print "[stdin read] You must answer Y/N and press ENTER"
    sys.stdout.flush()
    response = sys.stdin.read(1)
    print "You said '{}'".format(response)


def stdin_timeout():

    print "[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER"
    sys.stdout.flush()

    sys.stdin.flush()

    i, o, e = select.select( [sys.stdin], [], [], 10 )

    if (i):
        print "You said '{}'".format(sys.stdin.readline().strip())
        exit(0)
    else:
        print "You said nothing!"
        exit(1)


stdin_read()
stdin_timeout()

Python输出两个函数:

代码语言:javascript
复制
:~# python input.py
[stdin read] You must answer Y/N and press ENTER
n
You said 'n'
[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER
no
You said ''
:~# no
-bash: no: command not found

Python输出第二个函数

代码语言:javascript
复制
~# python input.py
[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER
no
You said 'no'
EN

回答 1

Stack Overflow用户

发布于 2016-01-28 20:06:08

这个问题来自于the behaviour of stdin flushing in unix

我将通过批注您的代码来回答:

代码语言:javascript
复制
import sys, select

def stdin_read():

    print "[stdin read] You must answer Y/N and press ENTER"
    # sys.stdout.flush() # not needed
    # you are reading one byte here, but the user enters at least two bytes
    # (e.g 'y' + LF(enter key)) so the LF byte (and possibly additional bytes)
    # remain in the stdin buffer.
    response = sys.stdin.read(1) # (->LF REMAINS)
    print "You said '{}'".format(response)


def stdin_timeout():

    print "[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER"
    #sys.stdout.flush() #  not needed
    # the behaviour of stdin flushing in unix is not defined (see 
    # link above)
    sys.stdin.flush()
    # thus the LF still remains in the stdin buffer

    # the select call blocks until the file discriptor is 'ready' for
    # the corresponding i/o operation. DISCLAIMER: assumptions are
    # following.... By default python does a open call which uses
    # buffered reads, so even if you did a read(1) it will likely read
    # more data from stdin and the select call blocks until the next LF
    # arrives.
    i, o, e = select.select( [sys.stdin], [], [], 10 )

    if (i):
        # you are just reading the left over bytes from above (->LF REMAINS)
        print "You said '{}'".format(sys.stdin.readline().strip())
        # Proof: readline() again:
        print "You missed '{}'".format(sys.stdin.readline().strip())
        exit(0)
    else:
        print "You said nothing!"
        exit(1)


stdin_read()
stdin_timeout()

但是也许使用另一种方法Keyboard input with timeout in Python会更好

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35018268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档