Mercurial > hg > config
comparison python/example/waiter.py @ 873:9f85111f3dee
add process waiting example
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 07 May 2019 10:40:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
872:633487219ed6 | 873:9f85111f3dee |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 wait for a process writing to stdout to keep an | |
5 (e.g.) ssh connection alive | |
6 """ | |
7 | |
8 import subprocess | |
9 import sys | |
10 import time | |
11 | |
12 KEEPALIVE = 300 # s | |
13 SLEEP = 1 | |
14 | |
15 | |
16 def main(args=sys.argv[1:]): | |
17 """CLI""" | |
18 | |
19 last = start = time.time() | |
20 proc = subprocess.Popen(args) | |
21 | |
22 while proc.poll() is None: | |
23 if time.time() - last > KEEPALIVE: | |
24 last = time.time() | |
25 print ("[{} s] waiting for process: {}".format(last-start, subprocess.list2cmdline(args))) | |
26 sys.stdout.flush() | |
27 exit(proc.poll()) | |
28 | |
29 if __name__ == '__main__': | |
30 main() |