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
|
2019-08-18 22:11:05 +00:00
|
|
|
from multiprocessing import Pool, freeze_support
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from functools import partial
|
|
|
|
import sys
|
2017-09-21 00:54:51 +00:00
|
|
|
|
|
|
|
|
2019-08-18 22:11:05 +00:00
|
|
|
L = list(range(9))[::-1]
|
2017-09-21 00:54:51 +00:00
|
|
|
|
|
|
|
|
2019-08-18 22:11:05 +00:00
|
|
|
def progresser(n, auto_position=False):
|
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)
|
2019-08-18 22:11:05 +00:00
|
|
|
for _ in tqdm(range(total), desc=text, position=None if auto_position else n):
|
2017-09-21 00:54:51 +00:00
|
|
|
sleep(interval)
|
2019-08-18 22:11:05 +00:00
|
|
|
# NB: may not clear instances with higher `position` upon completion
|
|
|
|
# since this worker may not know about other bars #796
|
|
|
|
if auto_position:
|
|
|
|
# we think we know about other bars (currently only py3 threading)
|
|
|
|
if n == 6:
|
|
|
|
tqdm.write("n == 6 completed")
|
|
|
|
|
|
|
|
if sys.version_info[:1] > (2,):
|
|
|
|
progresser_thread = partial(progresser, auto_position=True)
|
|
|
|
else:
|
|
|
|
progresser_thread = progresser
|
2017-09-21 00:54:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
freeze_support() # for Windows support
|
2019-08-18 22:11:05 +00:00
|
|
|
print("Multi-processing")
|
|
|
|
p = Pool(len(L), initializer=tqdm.set_lock, initargs=(tqdm.get_lock(),))
|
2017-09-21 00:54:51 +00:00
|
|
|
p.map(progresser, L)
|
|
|
|
|
2019-08-18 22:11:05 +00:00
|
|
|
# unfortunately need ncols
|
|
|
|
# to print spaces over leftover multi-processing bars (#796)
|
|
|
|
with tqdm(leave=False) as t:
|
|
|
|
ncols = t.ncols or 80
|
|
|
|
print(("{msg:<{ncols}}").format(msg="Multi-threading", ncols=ncols))
|
|
|
|
|
|
|
|
with ThreadPoolExecutor(4) as p:
|
|
|
|
p.map(progresser_thread, L)
|
2018-04-29 22:52:30 +00:00
|
|
|
|
2019-08-18 22:11:05 +00:00
|
|
|
print("Manual nesting")
|
|
|
|
for i in trange(16, desc="1"):
|
|
|
|
for _ in trange(16, desc="2 @ %d" % i, leave=i % 2):
|
2018-04-29 22:52:30 +00:00
|
|
|
sleep(0.01)
|