-
Notifications
You must be signed in to change notification settings - Fork 0
/
multibar.py
63 lines (51 loc) · 1.67 KB
/
multibar.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
import multiprocessing
import signal
from tqdm import tqdm
class TqdmBar:
tqdm = tqdm
total = None
items = None
desc = ''
def process_item(self, item):
"""
Operation to perform on a single item
"""
raise NotImplementedError()
def run(self, position=0):
for item in self.tqdm(
self.items,
total=self.total,
desc=self.desc,
position=position):
self.process_item(item)
class MultiBar:
tqdm = tqdm
def __init__(self, tqdmbar_list, threads, total=None, desc='Actions'):
"""
:param tqdmpbar_list: list[TqdmBar]
:param threads: int
:param total: int
"""
self.list = tqdmbar_list
self.threads = threads
self.total = total
self.desc = desc
def run(self):
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
pool = multiprocessing.Pool(processes=self.threads)
signal.signal(signal.SIGINT, original_sigint_handler)
try:
pool_generator = pool.imap_unordered(self.worker, self.list)
for response in self.tqdm(pool_generator,
total=self.total,
desc=self.desc, position=0):
pass
except KeyboardInterrupt:
pool.terminate()
else:
pool.close()
pool.join()
def worker(self, group_item):
current = multiprocessing.current_process()
position = current._identity[0]
group_item.run(position=position)