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
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! 123 456 Ok, ready, type something: abc [(0, 1)] >>> print(s) abc
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.