1 注意事项
- 使用map将无法显示进度,从0直接到100没有过程,使用imap可展示遍历过程。
- 不加list()无法正常显示进度条
方法 1
from multiprocessing import Pool
import tqdm
import time
def mypow(x):
square = x * x
time.sleep(0.1)
return square
with Pool(10) as p:
r = list(tqdm.tqdm(p.imap(mypow, range(30)), total=30, desc="mypow"))
方法 2
from multiprocessing import Pool
from progressbar import ProgressBar,Bar,ETA
from time import sleep
def myadd(n):
add = n + n
time.sleep(0.1)
return add
feed = range(256)
with Pool(10) as p:
widgets = [Bar(left="myadd: "),ETA()]
pbar = ProgressBar(widgets=widgets,maxval=len(feed))
res = list(pbar(p.imap(myadd,feed)))