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

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

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

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. chrisdude911
    Replies:
    10
    Views:
    135,429
    srk289
    Mar 10, 2009
  2. gaurav kashyap
    Replies:
    2
    Views:
    622
    gaurav kashyap
    Oct 30, 2008
  3. gaurav kashyap
    Replies:
    3
    Views:
    688
    gaurav kashyap
    Oct 31, 2008
  4. Mel
    Replies:
    10
    Views:
    3,128
    Sailaja Appi
    Feb 13, 2009
  5. Mark Probert

    Timeout::timeout and Socket timeout

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

Share This Page