Timeout at command prompt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thierry Lam

    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

  • Amit Khemka

    #2
    Re: Timeout at command prompt

    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(s ignal.SIGALRM, input)
    signal.alarm(TI MEOUT)

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


    cheers,
    amit.



    On 11 Jan 2006 13:24:34 -0800, Thierry Lam <lamthierry@gma il.com> wrote:[color=blue]
    > 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
    >[/color]


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

    Comment

    • Thierry Lam

      #3
      Re: Timeout at command prompt

      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\pciroo t\vcur\sdk\tool s\inter.py", line 32, in ?
      signal.signal(s ignal.SIGALRM, input)
      AttributeError: 'module' object has no attribute 'SIGALRM'


      Thierry

      Comment

      • Thierry Lam

        #4
        Re: Timeout at command prompt

        Is there a windows equivalent for that solution?

        Comment

        • Amit Khemka

          #5
          Re: Timeout at command prompt

          its "Python 2.4.1" .. on linux

          On 12 Jan 2006 06:34:08 -0800, Thierry Lam <lamthierry@gma il.com> wrote:[color=blue]
          > Is there a windows equivalent for that solution?
          >
          > --
          > http://mail.python.org/mailman/listinfo/python-list
          >[/color]


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

          Comment

          • Paul Rubin

            #6
            Re: Timeout at command prompt

            Amit Khemka <khemkaamit@gma il.com> writes:[color=blue]
            > import signal
            > TIMEOUT = 5 # number of seconds your want for timeout
            > signal.signal(s ignal.SIGALRM, input)
            > signal.alarm(TI MEOUT)
            >
            > def input():
            > try:
            > foo = raw_input()
            > return foo
            > except:
            > # timeout
            > return[/color]

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


            Comment

            • Amit Khemka

              #7
              Re: Timeout at command prompt

              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:[color=blue]
              > Amit Khemka <khemkaamit@gma il.com> writes:[color=green]
              > > import signal
              > > TIMEOUT = 5 # number of seconds your want for timeout
              > > signal.signal(s ignal.SIGALRM, input)
              > > signal.alarm(TI MEOUT)
              > >
              > > def input():
              > > try:
              > > foo = raw_input()
              > > return foo
              > > except:
              > > # timeout
              > > return[/color]
              >
              > This doesn't work with raw_input under linux, maybe because the
              > readline lib is snagging the timer interrupt or something. Use
              > sys.stdin.readl ine instead. SF bug:
              >
              > http://sourceforge.net/tracker/index...70&atid=105470
              > --
              > http://mail.python.org/mailman/listinfo/python-list
              >[/color]


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

              Comment

              • Amit Khemka

                #8
                Re: Timeout at command prompt

                Another thing that can be tried is:

                import threading
                a=""
                def input():
                global a
                a = raw_input()
                T = threading.Threa d(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 <khemkaamit@gma il.com> wrote:[color=blue]
                > 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:[color=green]
                > > Amit Khemka <khemkaamit@gma il.com> writes:[color=darkred]
                > > > import signal
                > > > TIMEOUT = 5 # number of seconds your want for timeout
                > > > signal.signal(s ignal.SIGALRM, input)
                > > > signal.alarm(TI MEOUT)
                > > >
                > > > def input():
                > > > try:
                > > > foo = raw_input()
                > > > return foo
                > > > except:
                > > > # timeout
                > > > return[/color]
                > >
                > > This doesn't work with raw_input under linux, maybe because the
                > > readline lib is snagging the timer interrupt or something. Use
                > > sys.stdin.readl ine instead. SF bug:
                > >
                > > http://sourceforge.net/tracker/index...70&atid=105470
                > > --
                > > http://mail.python.org/mailman/listinfo/python-list
                > >[/color]
                >
                >
                > --
                > ----
                > 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.
                >[/color]


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

                Comment

                • Thierry Lam

                  #9
                  Re: Timeout at command prompt

                  I got the signal to work on linux with sys.stdin.readl ine() 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

                  Comment

                  Working...