Skip to content

Instantly share code, notes, and snippets.

@shenfeng
Last active July 8, 2019 05:24
Show Gist options
  • Save shenfeng/f4a24778227df77687bb to your computer and use it in GitHub Desktop.
Save shenfeng/f4a24778227df77687bb to your computer and use it in GitHub Desktop.
def fork_and_wait(args, s):
p = subprocess.Popen(args, stdout=subprocess.PIPE)
def wait():
time.sleep(s)
if p.returncode is None:
logging.info("kill process %s", p.pid)
p.kill()
sys.exit(1)
elif p.returncode != 0:
logging.info("process return %d", p.returncode)
sys.exit(p.returncode)
t = threading.Thread(target=wait)
t.setDaemon(True)
t.start()
p.wait()
print p.stdout.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment