annotate python/multiproc.py @ 599:782dc37492c4

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 30 Jan 2014 12:45:45 -0800
parents c5537e841c78
children a77f7022cc06
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
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
14 progs = {'yes': ["yes"],
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
15 'ping': ['ping', 'google.com']}
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def main(args=sys.argv[1:]):
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
18 """CLI"""
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
20 # parse command line
596
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",
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
24 type=float, default=4.,
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
25 help="seconds to run for")
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
26 parser.add_argument("-s", "--sleep", dest="sleep",
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 type=float, default=1.,
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
28 help="")
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
31
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
32 # select program
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
33 prog = progs['ping']
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
34
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
35 # start the main subprocess loop
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
36 # TODO -> OO
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 output = tempfile.SpooledTemporaryFile()
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 start = time.time()
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 proc = subprocess.Popen(prog, stdout=output)
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
40 location = 0
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 while proc.poll() is None:
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
42 curr_time = time.time()
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
43 run_time = curr_time - start
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
44 if run_time > options.time:
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 proc.kill()
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
46 if options.sleep:
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
47 time.sleep(options.sleep)
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
48 output.seek(location)
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
49 read = output.read()
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
50 location += len(read)
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
51 print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10))
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 # reset tempfile
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 output.seek(0)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 n_lines = len(output.read().splitlines())
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines))
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 main()