当我把尺寸设为30码时,我一点也不跳。也许你有更多的处理器,可以让更多的工人运行。在
但是,如果n变大,您将开始看到跳跃,因为chunksize的性质。在
即
p.map将把你的输入分成chunksize并给每个进程一个chunk。所以当n变大,你的chunksize也会变大,你的。。。。。。。是的position(pos=n+1)!在
注意:尽管map保留了返回结果的顺序。其计算顺序是任意的。在
随着n的增长,我建议使用processor id作为每个进程查看进度的位置。在from time import sleep
import random
from tqdm import tqdm
from multiprocessing import Pool, freeze_support, RLock
from multiprocessing import current_process
def progresser(n):
text = f'#{n}'
sampling_counts = 10
current = current_process()
pos = current._identity[0]-1
with tqdm(total=sampling_counts, desc=text, position=pos) as pbar:
for i in range(sampling_counts):
sleep(random.uniform(0, 1))
pbar.update(1)
if __name__ == '__main__':
freeze_support()
L = list(range(30)) # works until 23, breaks starting at 24
# p = Pool(processes=None,
# initargs=(RLock(),), initializer=tqdm.set_lock
# )
with Pool(initializer=tqdm.set_lock, initargs=(tqdm.get_lock(),)) as p:
p.map(progresser, L)
print('\n' * (len(L) + 1))