Can I set timeout for the sys.stdin.readline() ?

Jay Parlar jparlar at cogeco.ca
Sun Jun 11 00:00:50 EDT 2006


On Jun 10, 2006, at 5:14 AM, ywzhan wrote:

> Hi, I am new here.
> When I use sys.stdin.readline() to get input string from user,
> how can I set a timeout value for this operation?
> thank you.
>
>

You can do a select() on sys.stdin, and put a timeout on the select, ie:

rfds, wfds, efds = select.select( [sys.stdin], [], [], 5)

would give you a five second timeout. If the timeout expired, then rfds 
would be an empty list. If the user entered something within five 
seconds, then rfds will be a list containing sys.stdin.

Note that by "user entered something", I mean they pressed 
Enter/Return. The select will not exit if they just enter other text.

Jay P.




More information about the Python-list mailing list