# HG changeset patch # User Jeff Hammel # Date 1557250828 25200 # Node ID 9f85111f3deeadaccb8671b6172fb06c432d92ee # Parent 633487219ed67cbd9d8f5927047ec1fbd5826840 add process waiting example diff -r 633487219ed6 -r 9f85111f3dee python/example/waiter.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/example/waiter.py Tue May 07 10:40:28 2019 -0700 @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +""" +wait for a process writing to stdout to keep an +(e.g.) ssh connection alive +""" + +import subprocess +import sys +import time + +KEEPALIVE = 300 # s +SLEEP = 1 + + +def main(args=sys.argv[1:]): + """CLI""" + + last = start = time.time() + proc = subprocess.Popen(args) + + while proc.poll() is None: + if time.time() - last > KEEPALIVE: + last = time.time() + print ("[{} s] waiting for process: {}".format(last-start, subprocess.list2cmdline(args))) + sys.stdout.flush() + exit(proc.poll()) + +if __name__ == '__main__': + main()