2017-09-21 00:54:51 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
from time import sleep
|
2018-04-29 22:52:30 +00:00
|
|
|
from tqdm import tqdm, trange
|
2017-10-15 18:19:04 +00:00
|
|
|
from multiprocessing import Pool, freeze_support, RLock
|
2017-09-21 00:54:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
L = list(range(9))
|
|
|
|
|
|
|
|
|
|
|
|
def progresser(n):
|
2017-11-07 00:10:09 +00:00
|
|
|
interval = 0.001 / (len(L) - n + 2)
|
2017-09-21 00:54:51 +00:00
|
|
|
total = 5000
|
|
|
|
text = "#{}, est. {:<04.2}s".format(n, interval * total)
|
2018-04-29 22:52:30 +00:00
|
|
|
# NB: ensure position>0 to prevent printing '\n' on completion.
|
|
|
|
# `tqdm` can't autmoate this since this thread
|
|
|
|
# may not know about other bars in other threads #477.
|
|
|
|
for _ in tqdm(range(total), desc=text, position=n + 1):
|
2017-09-21 00:54:51 +00:00
|
|
|
sleep(interval)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
freeze_support() # for Windows support
|
|
|
|
p = Pool(len(L),
|
|
|
|
initializer=tqdm.set_lock,
|
2017-10-15 18:19:04 +00:00
|
|
|
initargs=(RLock(),))
|
2017-09-21 00:54:51 +00:00
|
|
|
p.map(progresser, L)
|
2018-04-29 22:52:30 +00:00
|
|
|
print('\n' * len(L))
|
2017-09-21 00:54:51 +00:00
|
|
|
|
|
|
|
# alternatively, on UNIX, just use the default internal lock
|
|
|
|
p = Pool(len(L))
|
|
|
|
p.map(progresser, L)
|
2018-04-29 22:52:30 +00:00
|
|
|
print('\n' * len(L))
|
|
|
|
|
|
|
|
# a manual test demonstrating automatic fix for #477 on one thread
|
|
|
|
for _ in trange(10, desc="1", position=1):
|
|
|
|
for _ in trange(10, desc="2", position=0):
|
|
|
|
sleep(0.01)
|
|
|
|
print('\n')
|