Pergunta

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

Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top