【python|多程序】列印進度條
阿新 • • 發佈:2021-02-06
技術標籤:python
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()