Re: Problem: using signal.alarm() to stop a run-away os.system()command

Discussion in 'Python' started by Jp Calderone, May 27, 2005.

  1. Jp Calderone

    Jp Calderone Guest

    On 27 May 2005 12:09:39 -0700, wrote:
    >I'm trying to use signal.alarm to stop a run-away os.system command.
    >Can anyone exlain the following behavior?
    >
    >Given following the trivial program:
    >
    >import os
    >import signal
    >
    >def timeoutHandler(signum, frame):
    > print "Timeout"
    > raise ValueError
    >
    >
    >signal.signal(signal.SIGALRM, timeoutHandler)
    >signal.alarm(5)
    >
    >os.system("yes")
    >
    >signal.alarm(0)
    >
    >
    >What I expect is that the Linux/UNIX 'yes' command (which runs until
    >terminated) would be stopped after 5 seconds, when the timeoutHandler
    >is called, thus raising a ValueError and terminating this example
    >program. Instead, the 'yes' command run until it is terminated (by,
    >say, a kill command), at which time the timeoutHandler is called. In
    >other words, the running of the 'yes' command acts like it is blocking
    >the SIGALRM signal until it terminates, at which time the SIGALRM
    >signal is raised. This is quite surprising, and totally defeats my
    >ability to catch a run-away process. Can anyone see what I'm doing
    >wrong?


    CPython only delivers signals to Python programs in between bytecodes. Since your program is hanging around in the system(3) call, it isn't executing any bytecode, so CPython never gets an opportunity to deliver the signal.

    Try using a pipe (popen() or the new subprocess module) and select() with a timeout of 5.

    Jp
     
    Jp Calderone, May 27, 2005
    #1
    1. Advertising

  2. Jp Calderone

    Guest

    Re: Problem: using signal.alarm() to stop a run-away os.system() command

    Thanks, that's the kind of on-target info I needed. Now, shouldn't
    there be some kind of mention of this limitation in the docs?
     
    , May 27, 2005
    #2
    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. LeTubs

    is this an signal handler or a alarm ?

    LeTubs, Apr 30, 2004, in forum: C Programming
    Replies:
    3
    Views:
    369
    E. Robert Tisdale
    Apr 30, 2004
  2. Ishwar Rattan

    timeout using alarm signal question??

    Ishwar Rattan, Jul 22, 2004, in forum: Python
    Replies:
    0
    Views:
    365
    Ishwar Rattan
    Jul 22, 2004
  3. Ishwar Rattan

    timeout on on socket using alarm signal??

    Ishwar Rattan, Jul 22, 2004, in forum: Python
    Replies:
    0
    Views:
    622
    Ishwar Rattan
    Jul 22, 2004
  4. Replies:
    0
    Views:
    334
  5. Adrian Casey

    Using signal.alarm to terminate a thread

    Adrian Casey, Nov 13, 2006, in forum: Python
    Replies:
    7
    Views:
    614
    Nick Craig-Wood
    Nov 15, 2006
Loading...

Share This Page