擅长:python、mysql、java
<p>解决方法:小心!由于多处理,估计时间(每个循环的迭代次数、总时间等)可能不稳定,但进度条工作得很好。</p>
<p>注意:池的上下文管理器仅在Python3.3版中可用</p>
<pre><code>from multiprocessing import Pool
import time
from tqdm import *
def _foo(my_number):
square = my_number * my_number
time.sleep(1)
return square
if __name__ == '__main__':
with Pool(processes=2) as p:
max_ = 30
with tqdm(total=max_) as pbar:
for i, _ in tqdm(enumerate(p.imap_unordered(_foo, range(0, max_)))):
pbar.update()
</code></pre>