-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmulti_progress.py
130 lines (104 loc) · 3.36 KB
/
multi_progress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import division
import sys
import time
import random
from multiprocessing import Pool
from blessings import Terminal
from progressbar import ProgressBar
term = Terminal()
class Writer(object):
"""Create an object with a write method that writes to a
specific place on the screen, defined at instantiation.
This is the glue between blessings and progressbar.
"""
def __init__(self, location):
"""
Input: location - tuple of ints (x, y), the position
of the bar in the terminal
"""
self.location = location
def write(self, string):
with term.location(*self.location):
print(string)
def test(location):
"""Test with a single bar.
Input: location - tuple (x, y) defining the position on the
screen of the progress bar
"""
# fd is an object that has a .write() method
writer = Writer(location)
pbar = ProgressBar(fd=writer)
# progressbar usage
pbar.start()
for i in range(100):
# do stuff
# time taken for process is function of line number
# t_wait = location[1] / 100
# time take is random
t_wait = random.random() / 50
time.sleep(t_wait)
# update calls the write method
pbar.update(i)
pbar.finish()
def test_bars(locations):
"""Test with multiple bars.
Input: locations - a list of location (x, y) tuples
"""
writers = [Writer(loc) for loc in locations]
pbars = [ProgressBar(fd=writer) for writer in writers]
for pbar in pbars:
pbar.start()
for i in range(100):
time.sleep(0.01)
for pbar in pbars:
pbar.update(i)
for pbar in pbars:
pbar.finish()
def test_parallel(locations):
"""Test case with multiprocessing.
Input: locations - a list of location (x, y) tuples
Think of locations as a list of jobs. Each job is going
to be processed by the single bar test.
"""
pool = Pool()
pool.map(test, locations)
pool.close()
def main():
if len(sys.argv) == 4 and sys.argv[1] == 'single':
x = int(sys.argv[2])
y = int(sys.argv[3])
print("Printing at ({x}, {y})".format(x=x, y=y))
location = (x, y)
test(location)
elif len(sys.argv) == 4 and sys.argv[1] == 'multi':
first = int(sys.argv[2])
last = int(sys.argv[3])
locations = [(0, i) for i in range(first, last)]
test_bars(locations)
elif len(sys.argv) == 4 and sys.argv[1] == 'parallel_single':
line1 = int(sys.argv[2])
line2 = int(sys.argv[3])
locations = [(0, line1), (0, line2)]
test_parallel(locations)
elif len(sys.argv) == 4 and sys.argv[1] == 'parallel_multi':
first = int(sys.argv[2])
last = int(sys.argv[3])
locations = [(0, i) for i in range(first, last)]
test_parallel(locations)
else:
raise UserWarning
def usage():
"""Print help to the screen"""
usage_str = """Usage:
python multi.py single x_pos y_pos
python multi.py multi first_line last_line
python multi.py parallel_single line_one line_two
python multi.py parallel_multi first_line last_line
"""
print(usage_str)
if __name__ == '__main__':
try:
with term.fullscreen():
main()
except UserWarning:
usage()