2

My python script calls a batch file which starts a network throughput test (ixChariot, like Iperf) via command line.

As I increase the attenuation in this RF test in order to test throughput, the ixChariot begins to take forever and does not respond to the initial test duration parameter. Therefore, slowing the entire script down to a halt (or I should say making it last forever)

How can I end the ixChariot process and end the test after time T has passed so that my python script doesn't hang from Popen?

Thanks guys, this one is a toughy for me

coolestDisplayName
  • 161
  • 1
  • 2
  • 9
  • All you do is time the process of the batch, like this, initialize `startTime` before the batch, and when the batch process no longer exists, initialize a new `endTime`, then do `finalTime = endTime - startTime` –  Aug 22 '13 at 19:03
  • Thanks! The problem is when the external program is run from the batch, the batch won't stop because the external program is not ending. Typically, the throughput program lasts 30 seconds but as I increase the attenuation and throttle that throughput, the program slows down to a halt but doesn't actually end. Thus, the batch does not end either. – coolestDisplayName Aug 22 '13 at 20:06

1 Answers1

1

You can try to put an alarm around the process

from signal import alarm, signal, SIGALRM, SIGKILL

TIMEOUT=-9

def run_proc_timeout(proc, timeout):

    class _Alarm(Exception):
        pass

    def alarm_handler(signum, frame):
        raise _Alarm

    # setup alarm for timeout length
    signal(SIGALRM, alarm_handler)
    alarm(timeout)

    try:
        stdout, stderr = proc.communicate()
        alarm(0)
    except _Alarm:
        pids = [proc.pid]
        # kill proc and all its children
        return TIMEOUT, "", ""

    return proc.returncode, stdout, stderr
nitekrawler
  • 425
  • 3
  • 8
  • Interesting! So my subprocess would like like: p = subprocess.Popen(BATCH, shell=True) returncode[0] = run_proc_timeout(p, # seconds) stdout, stderr = p.communicate() Is that along the right lines? – coolestDisplayName Aug 22 '13 at 20:29
  • If I understand what you correctly, you should be able to run `p=subprocess.Popen(BATCH, shell=True); (returncode, stdout, stderr)=run_proc_timeout(p, # seconds)`. In the case of timeout, it will return (-9, "", "")--I just noticed I forgot to include TIMEOUT=-9, this is now fixed. If it doesn't timeout, you'll get the (returncode, stdout, stderr) from your batch process. – nitekrawler Aug 22 '13 at 20:57