I'm having trouble understanding select.select behavior. Please consider the following Python program:
def str_to_hex(s): def dig(n): if n > 9: return chr(65-10+n) else: return chr(48+n) r = '' while len(s) > 0: c = s[0] s = s[1:] a = ord(c) / 16 b = ord(c) % 16 r = r + dig(a) + dig(b) return r while True: ans,_,_ = select.select([sys.stdin],[],[]) print ans s = ans[0].read(1) if len(s) == 0: break print str_to_hex(s)
I saved this in test.py file. If you call it like this:
echo 'hello' | ./test.py
then I get the expected behavior: choose never block and all data is printed; the program ends.
But if I run the program interactively, I get the most unwanted behavior. Please consider the following console session:
$ ./test.py hello [<open file '<stdin>', mode 'r' at 0xb742f020>] 68
Then the program hangs there; select.select now blocks again. Until I have provided more input or closed the input stream so that the next character (and all the rest) is printed, although there are already characters waiting! Can someone explain this behavior to me? I see something like this in the stream tunneling program I wrote, and it ruins the whole thing.
Thank you for reading!
tvynr source share