comparison python/multiproc.py @ 596:c5537e841c78

wip
author Jeff Hammel <k0scist@gmail.com>
date Wed, 29 Jan 2014 11:12:09 -0800
parents
children 782dc37492c4
comparison
equal deleted inserted replaced
595:4b79ee6c8539 596:c5537e841c78
1 #!/usr/bin/env python
2
3 """
4 multiprocessing/subprocess experiments
5 """
6
7 import argparse
8 import os
9 import subprocess
10 import sys
11 import time
12 import tempfile
13
14 here = os.path.dirname(os.path.realpath(__file__))
15 prog = ["yes"]
16 timeout = 4.
17 sleep = 1.
18
19 def main(args=sys.argv[1:]):
20
21 usage = '%prog [options]'
22 parser = argparse.ArgumentParser(usage=usage, description=__doc__)
23 parser.add_argument("-t", "--time", dest="time",
24 type=float, default=1.,
25 help="seconds to run for")
26 options = parser.parse_args(args)
27
28 output = tempfile.SpooledTemporaryFile()
29 start = time.time()
30 proc = subprocess.Popen(prog, stdout=output)
31 while proc.poll() is None:
32 if time.time() - start > options.time:
33 proc.kill()
34
35 # reset tempfile
36 output.seek(0)
37
38 n_lines = len(output.read().splitlines())
39 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines))
40
41 if __name__ == '__main__':
42 main()