every blog every motto: What doesn’t kill you makes you stronger.
0. 前言
在多进程中使用进度条
1. 正文
1.1 普通情况
import time
from tqdm import tqdm
for i in tqdm(range(1000)):
time.sleep(0.01)
1.2 多进程
1.2.1 未打印进度条
import time
from tqdm import tqdm
from multiprocessing.pool import ThreadPool
def fun():
"""测试函数"""
time.sleep(0.01)
pool = ThreadPool(4)
for i in range(1000):
pool.apply_async(fun)
1.2.2 打印进度条
import time
from tqdm import tqdm
from multiprocessing.pool import ThreadPool
def fun():
"""测试函数"""
time.sleep(0.01)
num = 1000
pbar = tqdm(total=num)
update = lambda *args: pbar.update()
pool = ThreadPool(4)
for i in range(num):
pool.apply_async(fun, callback=update)
pool.close()
pool.join()