擅长:python、mysql、java
<p>根据Xavi martinez的回答,我编写了函数<code>imap_unordered_bar</code>。它可以与<code>imap_unordered</code>以相同的方式使用,唯一的区别是显示了一个处理条。</p>
<pre><code>from multiprocessing import Pool
import time
from tqdm import *
def imap_unordered_bar(func, args, n_processes = 2):
p = Pool(n_processes)
res_list = []
with tqdm(total = len(args)) as pbar:
for i, res in tqdm(enumerate(p.imap_unordered(func, args))):
pbar.update()
res_list.append(res)
pbar.close()
p.close()
p.join()
return res_list
def _foo(my_number):
square = my_number * my_number
time.sleep(1)
return square
if __name__ == '__main__':
result = imap_unordered_bar(_foo, range(5))
</code></pre>