How do I read everything there is from the stdin buffer in python without blocking? None of the stack overflow solutions I found work as my stdin is non-interactive.
I tried:
tcflush
tcflush(sys.stdin, TCIOFLUSH) which does not work on non-interactive stdin.
select
while select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
sys.stdin.read(1)
and
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
os.read(sys.stdin.fileno(), 4096)
Which do not work because the condition in the while head is always False.
Any other ideas?
Edit
To be more precise:
My code should
- do nothing if the input buffer is empty
- empty the buffer if there is something in it
So it should never block.
input()after thewhilestatement, it does not block. Theinput()also does not block if put before thewhilestatement so I am pretty sure there is something in the buffer.selectfunctionality in Windows is provided by WinSock, and thus only works on sockets. See the documentation.