Clear sys.stdin Buffer

With epoll() it is possible to detect whether sys.stdin (the standard input character stream) is readable (i.e. someone typed something and concluded with <enter>). Standard input then can be obtained by calling sys.stdin.readline().

But it is not that trivial to get the last input line because sys.stdin is buffered. The user might have entered some text at a point of time when it was not expected yet. Of course one might use the input() function to get user input, but it blocks until the input is concluded. This behaviour is not suitable in an asynchronous I/O architecture where epoll() is used to get notifications of new input.

Thus one needs a function to clear the buffer of stdin when the program expects user input — i.e. when one would usually present some kind of prompt. But how would one do it? There were some questions on the web, but answers were rare. Here’s my answer: use termios.tcflush()! Citing the Python help on tcflush():

tcflush(fd, queue) -> None

Discard queued data on file descriptor fd.
The queue selector specifies which queue: termios.TCIFLUSH for the input
queue, termios.TCOFLUSH for the output queue, or termios.TCIOFLUSH for
both queues.

Consider the following Python interpreter snippet:

>>> import select,sys,time,termios
>>> epoll = select.epoll()
>>> epoll.register(sys.stdin,select.EPOLLIN)
>>> print("not yet ready for input, but go ahead!"); \
... time.sleep(3); \
... termios.tcflush(sys.stdin,termios.TCIFLUSH); \
... print("Ok, ready, type something:"); \
... sys.stdout.flush(); \
... epoll.poll(); \
... s = sys.stdin.readline().strip()
not yet ready for input, but go ahead!
Ok, ready, type something:
[(0, 1)]
>>> print(s)

During the three second sleep I typed 123<enter>456<enter>. Then the interpreter waited until “abc!” was typed. In the end this string could be retrieved from stdout.

I’ll use this approach for the “stdio” module of my control system.


One thought on “Clear sys.stdin Buffer

  1. Thanks!
    Just a question, do you know why this doesn’t work in a pipe?
    eg) running ./ | ./
    I keep getting termios.error 22 invalid argument …

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s