annotate python/multiproc.py @ 603:bd30deecdf4a

STUB: python/multiproc.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 30 Jan 2014 16:22:26 -0800
parents 330e3435a7d7
children 5b561c7b2005
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
601
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
14 class Process(subprocess.Popen):
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
15 """why would you name a subprocess object Popen?"""
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
16
603
bd30deecdf4a STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 602
diff changeset
17 defaults = {'buffsize': 'line buffered'
bd30deecdf4a STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 602
diff changeset
18 }
bd30deecdf4a STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 602
diff changeset
19
601
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
20 def __init__(self, *args, **kwargs):
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
21 self.output = tempfile.SpooledTemporaryFile()
7f189613a289 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 600
diff changeset
22 subprocess.Popen.__init__(self, *args, **kwargs)
602
330e3435a7d7 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 601
diff changeset
23 # TODO: finish
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 def main(args=sys.argv[1:]):
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
26 """CLI"""
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
28 # available programs
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
29 progs = {'yes': ["yes"],
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
30 'ping': ['ping', 'google.com']}
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
31
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
32
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
33 # parse command line
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 usage = '%prog [options]'
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 parser = argparse.ArgumentParser(usage=usage, description=__doc__)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 parser.add_argument("-t", "--time", dest="time",
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
37 type=float, default=4.,
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
38 help="seconds to run for")
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
39 parser.add_argument("-s", "--sleep", dest="sleep",
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 type=float, default=1.,
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
41 help="sleep this number of seconds between polling")
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
42 parser.add_argument("-p", "--prog", dest='program',
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
43 choices=progs.keys(),
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
44 help="subprocess to run")
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
45 # TODO parser.add_argument("--list-programs", help="list available programs")
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
48
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
49 # select program
600
a77f7022cc06 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 599
diff changeset
50 prog = progs[options.program]
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
51
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
52 # start the main subprocess loop
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
53 # TODO -> OO
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 output = tempfile.SpooledTemporaryFile()
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 start = time.time()
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 proc = subprocess.Popen(prog, stdout=output)
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
57 location = 0
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 while proc.poll() is None:
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
59 curr_time = time.time()
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
60 run_time = curr_time - start
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
61 if run_time > options.time:
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 proc.kill()
599
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
63 if options.sleep:
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
64 time.sleep(options.sleep)
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
65 output.seek(location)
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
66 read = output.read()
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
67 location += len(read)
782dc37492c4 STUB: python/multiproc.py
Jeff Hammel <k0scist@gmail.com>
parents: 596
diff changeset
68 print ('[{}] {}\n{}'.format(run_time, read, '-==-'*10))
596
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 # reset tempfile
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 output.seek(0)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 n_lines = len(output.read().splitlines())
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 print ("{}: {} lines".format(subprocess.list2cmdline(prog), n_lines))
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 main()