如何在python中刷新输入流?

Amj*_*ith 14 python

我正在用Python编写一个简单的报警实用程序.

#!/usr/bin/python

import time
import subprocess
import sys

alarm1 = int(raw_input("How many minutes (alarm1)? "))

while (1):
    time.sleep(60*alarm1)
    print "Alarm1"
    sys.stdout.flush()
    doit = raw_input("Continue (Y/N)?[Y]: ")
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
Run Code Online (Sandbox Code Playgroud)

我想刷新或丢弃脚本处于休眠状态时输入的所有键击,并且只在执行raw_input()后接受键击.

编辑:我在Windows XP上运行它.

Pet*_*ley 11

了解您正在使用的操作系统将有所帮助,因为这是一个特定于操作系统的问题.例如,Kylar的答案在Windows上不起作用,因为sys.stdin没有fileno属性.

我很好奇,并使用curses将解决方案整合在一起,但这在Windows上也不起作用:

#!/usr/bin/python                                                               

import time
import sys
import curses

def alarmloop(stdscr):
    stdscr.addstr("How many seconds (alarm1)? ")
    curses.echo()
    alarm1 = int(stdscr.getstr())
    while (1):
        time.sleep(alarm1)
        curses.flushinp()
        stdscr.clear()
        stdscr.addstr("Alarm1\n")
        stdscr.addstr("Continue (Y/N)?[Y]:")
        doit = stdscr.getch()
        stdscr.addstr("\n")
        stdscr.addstr("Input "+chr(doit)+"\n")
        stdscr.refresh()
        if doit == ord('N') or doit == ord('n'):
            stdscr.addstr("Exiting.....\n")
            break

curses.wrapper(alarmloop)
Run Code Online (Sandbox Code Playgroud)

编辑:啊,Windows.然后您可以使用msvcrt模块.请注意,下面的代码并不完美,它在IDLE中根本不起作用:

#!/usr/bin/python

import time
import subprocess
import sys
import msvcrt

alarm1 = int(raw_input("How many seconds (alarm1)? "))

while (1):
    time.sleep(alarm1)
    print "Alarm1"
    sys.stdout.flush()

    # Try to flush the buffer
    while msvcrt.kbhit():
        msvcrt.getch()

    print "Continue (Y/N)?[Y]"
    doit = msvcrt.getch()
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
Run Code Online (Sandbox Code Playgroud)


Kyl*_*lar 6

#!/usr/bin/python

import time
import subprocess
import sys
import os, select

alarm1 = int(raw_input("How many minutes (alarm1)? "))

while (1):
    time.sleep(3*alarm1)
    print "Alarm1"
    sys.stdout.flush()
    while select.select([sys.stdin.fileno()], [], [], 0.0)[0]:
        os.read(sys.stdin.fileno(), 4096)
    doit = raw_input("Continue (Y/N)?[Y]: ")
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
Run Code Online (Sandbox Code Playgroud)

  • 啊,我不知道你在窗户上。这根本不起作用(我看到了 /usr/bin/python 并假设。我的错。) select 的文档在这里:http://docs.python.org/library/select.html 但在 Windows 上它只会绑定到一个套接字。 (4认同)

Cal*_*had 6

在Unices上,您可以使用termios.tcflush():

import time
import subprocess
import sys
from termios import tcflush, TCIOFLUSH

alarm1 = int(raw_input("How many minutes (alarm1)? "))

while (1):
    time.sleep(60*alarm1)
    print "Alarm1"
    sys.stdout.flush();
    tcflush(sys.stdin, TCIOFLUSH)
    doit = raw_input("Continue (Y/N)?[Y]: ")
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
Run Code Online (Sandbox Code Playgroud)


kol*_*ery 6

来自Rosetta Code

def flush_input():
    try:
        import msvcrt
        while msvcrt.kbhit():
            msvcrt.getch()
    except ImportError:
        import sys, termios    #for linux/unix
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)
Run Code Online (Sandbox Code Playgroud)

试用部分适用于Windows平台.我没有亲自测试过这部分.但是除了部分适用于linux终端.termios模块具有一些终端接口功能.tcflush可以刷新输入或输出缓冲数据.这部分肯定适用于我的测试.

  • @bub,谢谢你的暗示.我认为这个问题不需要太多的解释.将编辑我的答案 (2认同)