Page 1 of 1

select.select( [sys.stdin], [], [], 10 ) does not work

Posted: Tue Nov 15, 2016 12:48 pm
by keattisak
Hello everyone,

The following is the famous python code for checkin hitting of keyboard.

Code: Select all

import sys, select

print "You have ten seconds to answer!"
i, o, e = select.select( [sys.stdin], [], [], 10 )
if (i):
  print "You said", sys.stdin.readline().strip()
else:
  print "You said nothing!"
I boot up my Pi3 (jessie) in console mode(command line).
Then, by typing in the command line, the code above run correctly with 10 seconds of timeout.

But if I start the code automatically at the boot up by using bash file associated with rc.local.
It always pass to "print "You said", sys.stdin.readline().strip()" without waiting of timeout !!!.

I tested it many time, It looks like there is some thing in i.
I did sys.stdin.flush() or read once before i, o, e = select.select( [sys.stdin], [], [], 10 )
but it does not work. There is something in i and no wait of timeout.

Later, if I manually run, the code run correctly as expected.

Thank you for your help.
Keattisak.

Re: select.select( [sys.stdin], [], [], 10 ) does not work

Posted: Tue Nov 15, 2016 7:24 pm
by thagrol
Running from rc.local means the stdin isn't connected to anything.

My guess is that in this case the select returns immediately with sys.stdin in 'e' and nothing in 'i' and 'o'. Your output is likely to be lost too as stdout won't be connected to anything either.

Re: select.select( [sys.stdin], [], [], 10 ) does not work

Posted: Wed Nov 16, 2016 3:08 am
by keattisak
thagrol wrote:Running from rc.local means the stdin isn't connected to anything.

My guess is that in this case the select returns immediately with sys.stdin in 'e' and nothing in 'i' and 'o'. Your output is likely to be lost too as stdout won't be connected to anything either.
Thank you for your reply.

I dont know much about pi but I think you are correct. The select returns immediately with nothing in 'i'.

I noticed that 'rc.local' was run before loging in to pi.
Then, I run my script after login in by using /etc/profile and it works perfectly now.

The problem is why the code likes this working well on Pi 2 with Wheezy but it doesnot on pi 3 with Jessie.

:roll: