Timeout at command prompt

T

Thierry Lam

I can use the python function raw_input() to read any input from the
user but how can I add a timeout so that my program exits after x
period of time when no input has been entered.

Thierry
 
A

Amit Khemka

One way would be to use 'signal' s ... something like this should work

import signal
TIMEOUT = 5 # number of seconds your want for timeout
signal.signal(signal.SIGALRM, input)
signal.alarm(TIMEOUT)

def input():
try:
foo = raw_input()
return foo
except:
# timeout
return


cheers,
amit.



I can use the python function raw_input() to read any input from the
user but how can I add a timeout so that my program exits after x
period of time when no input has been entered.

Thierry


--
 
T

Thierry Lam

Which Python version are you using? I'm getting the following error
with Python 2.3.4:

Traceback (most recent call last):
File "C:\home\pciroot\vcur\sdk\tools\inter.py", line 32, in ?
signal.signal(signal.SIGALRM, input)
AttributeError: 'module' object has no attribute 'SIGALRM'


Thierry
 
P

Paul Rubin

A

Amit Khemka

Another thing that can be tried is:

import threading
a=""
def input():
global a
a = raw_input()
T = threading.Thread(target=input)
T.start()
T.join(2) ## does the trick
...

I have not tested it but i guess should work.

cheers,
amit.

I tried it on "Python 2.4.1" on '2.6.11-1.1369_FC4smp with gcc version
4.0.0' .. which works fine .. may be it could be an issue with some
other combinations ..

cheers,
amit




--
----
Endless the world's turn, endless the sun's spinning
Endless the quest;
I turn again, back to my own beginning,
And here, find rest.


--
 
T

Thierry Lam

I got the signal to work on linux with sys.stdin.readline() but the
process timeout after x seconds even when I input something. Is there
a way to close the signal after getting a correct input from the
console window?

Thanks
Thierry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top