473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Timeout at command prompt

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

Jan 11 '06 #1
8 5773
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 <la********@gmail.com> 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.
Jan 12 '06 #2
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

Jan 12 '06 #3
Is there a windows equivalent for that solution?

Jan 12 '06 #4
its "Python 2.4.1" .. on linux

On 12 Jan 2006 06:34:08 -0800, Thierry Lam <la********@gmail.com> 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.
Jan 12 '06 #5
Amit Khemka <kh********@gmail.com> 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
Jan 12 '06 #6
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 <kh********@gmail.com> 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.
Jan 12 '06 #7
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 <kh********@gmail.com> 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 <kh********@gmail.com> 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.
Jan 12 '06 #8
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

Jan 12 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Adrian Casey | last post by:
I have a python script which uses pexpect and I want to timeout (i.e. raise pexpect.TIMEOUT) if a long running command does not produce the output I am expecting. To simulate the 'long running...
4
by: Cath B | last post by:
I am pretty sure I am getting a command timeout when execute a SQL procedure that has an output parameter. The code below is in an asp page that is called using RSGetASPObject. I want to be able...
4
by: glenn | last post by:
I keep reading all sorts of books on VS that keep telling me to click on Tools/Visual Studio Command prompt to run this program or that program. However, I do not have such a menu choice. Where is...
5
by: Jason | last post by:
Hi all I get the following error when executing a rather intense stored procedure from an ASPX page. I have tried: - Increasing timeouts on IIS 5.0 (all areas that even mention timeout) - use...
10
by: Will Gillen | last post by:
I have an ASP.NET application that is using Windows Integrated Authentication (IIS) (as opposed to Forms Authentication). When the user first logs into the application, IIS prompts the user for...
13
by: Mark A. Nadig | last post by:
I've got a console application in vb.net to replicate the behavior of the old DOS command CHOICE. I've got a timer event successfully firing, however the main thread is stuck on the console.read()....
0
by: bonita | last post by:
If I add the code for user to download the file (e.g. if(File.Exists(FILE_NAME)){......}), the ASP.NET will give the following timeout error: Timeout expired. The timeout period elapsed prior to...
22
by: pb648174 | last post by:
After executing osql from the command line via the Windows scheduled task interface, the following error is returned: "Timeout expired" The code in the sql is as follows: BACKUP Database...
0
by: SteveBark | last post by:
Any pointers on this truly appreciated. I am using net::telnet to connect to a modem pool and then connect with a remote piece of kit. Everything works fine normally, however on occassions the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.