annotate python/multiproc.py @ 602:330e3435a7d7

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