Timeout at command prompt

Discussion in 'Python' started by Thierry Lam, Jan 11, 2006.

  1. Thierry Lam

    Thierry Lam Guest

    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
     
    Thierry Lam, Jan 11, 2006
    #1
    1. Advertisements

  2. Thierry Lam

    Amit Khemka Guest

    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 <> 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.
     
    Amit Khemka, Jan 12, 2006
    #2
    1. Advertisements

  3. Thierry Lam

    Thierry Lam Guest

    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
     
    Thierry Lam, Jan 12, 2006
    #3
  4. Thierry Lam

    Thierry Lam Guest

    Is there a windows equivalent for that solution?
     
    Thierry Lam, Jan 12, 2006
    #4
  5. Thierry Lam

    Amit Khemka Guest

    its "Python 2.4.1" .. on linux

    On 12 Jan 2006 06:34:08 -0800, Thierry Lam <> 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.
     
    Amit Khemka, Jan 12, 2006
    #5
  6. Thierry Lam

    Paul Rubin Guest

    Amit Khemka <> 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.php?func=detail&aid=685846&group_id=5470&atid=105470
     
    Paul Rubin, Jan 12, 2006
    #6
  7. Thierry Lam

    Amit Khemka Guest

    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 <> 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.php?func=detail&aid=685846&group_id=5470&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.
     
    Amit Khemka, Jan 12, 2006
    #7
  8. Thierry Lam

    Amit Khemka Guest

    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 <> 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 <> 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.php?func=detail&aid=685846&group_id=5470&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.
     
    Amit Khemka, Jan 12, 2006
    #8
  9. Thierry Lam

    Thierry Lam Guest

    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
     
    Thierry Lam, Jan 12, 2006
    #9
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth Brun Nielsen

    Synopsys Design Analyzer in command prompt

    Kenneth Brun Nielsen, May 9, 2005, in forum: VHDL
    Replies:
    6
    Views:
    3,262
    Kenneth Brun Nielsen
    May 9, 2005
  2. Bobby Ryzhy
    Replies:
    1
    Views:
    507
    =?Utf-8?B?T2xlZw==?=
    Aug 4, 2004
  3. Replies:
    3
    Views:
    2,575
    John Timney \(ASP.NET MVP\)
    Apr 26, 2005
  4. chrisdude911
    Replies:
    10
    Views:
    136,175
    srk289
    Mar 10, 2009
  5. gaurav kashyap
    Replies:
    2
    Views:
    832
    gaurav kashyap
    Oct 30, 2008
  6. gaurav kashyap
    Replies:
    3
    Views:
    882
    gaurav kashyap
    Oct 31, 2008
  7. Mel
    Replies:
    10
    Views:
    3,539
    Sailaja Appi
    Feb 13, 2009
  8. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,631
    Brian Candler
    Oct 6, 2004
Loading...