annotate python/multiproc.py @ 598:e21c49e32b95

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