【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()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39190382/article/details/112621211