Snippets

Steven Berlan Keep an application running (but respect ctrl+c)

Created by Steven Berlan
#!/usr/bin/env python
import subprocess
import sys
from time import time

try:
    code, runtimes = 0, []
    while len(runtimes) < 3 or (sum(runtimes) / 3) > 0.5:
        start = time()
        handle = subprocess.Popen(sys.argv[1:], stderr=sys.stderr, stdin=sys.stdin, stdout=sys.stdout)
        code = handle.wait()
        if len(runtimes) > 2:
            runtimes = runtimes[1:] + [time() - start]
        else:
            runtimes.append(time() - start)
except KeyboardInterrupt:
    print ''
    try:
        handle.send_signal(2)
    except:
        pass
except OSError:
    pass

sys.exit(code)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.