1from __future__ import print_function 2import os 3import subprocess 4import sys 5import threading 6 7def kill_subprocess(process): 8 process.kill() 9 os._exit(1) 10 11# Pass -f=none and --output-style=GNU to get only one line of output per input. 12cmd = subprocess.Popen([sys.argv[1], 13 '--obj=' + sys.argv[2], 14 '-f=none', 15 '--output-style=GNU'], stdout=subprocess.PIPE, stdin=subprocess.PIPE) 16watchdog = threading.Timer(20, kill_subprocess, args=[cmd]) 17watchdog.start() 18cmd.stdin.write(b'0\n') 19cmd.stdin.flush() 20print(cmd.stdout.readline()) 21cmd.stdin.write(b'bad\n') 22cmd.stdin.flush() 23print(cmd.stdout.readline()) 24watchdog.cancel() 25