Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Timeout at command prompt

Reply
Thread Tools

Timeout at command prompt

 
 
Thierry Lam
Guest
Posts: n/a
 
      01-11-2006
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

 
Reply With Quote
 
 
 
 
Amit Khemka
Guest
Posts: n/a
 
      01-12-2006
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.



On 11 Jan 2006 13:24:34 -0800, Thierry Lam <(E-Mail Removed)> wrote:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
----
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.
 
Reply With Quote
 
 
 
 
Thierry Lam
Guest
Posts: n/a
 
      01-12-2006
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

 
Reply With Quote
 
Thierry Lam
Guest
Posts: n/a
 
      01-12-2006
Is there a windows equivalent for that solution?

 
Reply With Quote
 
Amit Khemka
Guest
Posts: n/a
 
      01-12-2006
its "Python 2.4.1" .. on linux

On 12 Jan 2006 06:34:08 -0800, Thierry Lam <(E-Mail Removed)> wrote:
> Is there a windows equivalent for that solution?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
----
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.
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-12-2006
Amit Khemka <(E-Mail Removed)> writes:
> 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


This doesn't work with raw_input under linux, maybe because the
readline lib is snagging the timer interrupt or something. Use
sys.stdin.readline instead. SF bug:

http://sourceforge.net/tracker/index...70&atid=105470
 
Reply With Quote
 
Amit Khemka
Guest
Posts: n/a
 
      01-12-2006
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

On 12 Jan 2006 07:35:57 -0800, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> Amit Khemka <(E-Mail Removed)> writes:
> > 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

>
> This doesn't work with raw_input under linux, maybe because the
> readline lib is snagging the timer interrupt or something. Use
> sys.stdin.readline instead. SF bug:
>
> http://sourceforge.net/tracker/index...70&atid=105470
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
----
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.
 
Reply With Quote
 
Amit Khemka
Guest
Posts: n/a
 
      01-12-2006
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.

On 1/12/06, Amit Khemka <(E-Mail Removed)> wrote:
> 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
>
> On 12 Jan 2006 07:35:57 -0800, Paul Rubin
> <"http://phr.cx"@nospam.invalid> wrote:
> > Amit Khemka <(E-Mail Removed)> writes:
> > > 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

> >
> > This doesn't work with raw_input under linux, maybe because the
> > readline lib is snagging the timer interrupt or something. Use
> > sys.stdin.readline instead. SF bug:
> >
> > http://sourceforge.net/tracker/index...70&atid=105470
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >

>
>
> --
> ----
> 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.
>



--
----
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.
 
Reply With Quote
 
Thierry Lam
Guest
Posts: n/a
 
      01-12-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javac command not working in command prompt chrisdude911 Java 10 03-10-2009 09:55 AM
How to open a shell prompt from an existing shell prompt gaurav kashyap Python 3 10-31-2008 04:41 AM
How to open a shell prompt from an existing shell prompt gaurav kashyap Python 2 10-30-2008 09:18 AM
Access Denied when i type command at command prompt calvin MCDST 0 03-08-2006 06:33 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM



Advertisments