多重处理:使用tqdm显示进度条

编程入门 行业动态 更新时间:2024-10-28 11:34:41
本文介绍了多重处理:使用tqdm显示进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为了使我的代码更"Pythonic"且更快,我使用"multiprocessing"和一个map函数向其发送a)函数和b)迭代范围.

To make my code more "pythonic" and faster, I use "multiprocessing" and a map function to send it a) the function and b) the range of iterations.

植入的解决方案(即直接在tqdm.tqdm(range(0,30))范围内调用tqdm)不适用于多重处理(如下代码所示).

The implanted solution (i.e., call tqdm directly on the range tqdm.tqdm(range(0, 30)) does not work with multiprocessing (as formulated in the code below).

进度条显示为0到100%(当python读取代码时?),但是它并不表示map函数的实际进度.

The progress bar is displayed from 0 to 100% (when python reads the code?) but it does not indicate the actual progress of the map function.

如何显示进度条以指示地图"功能在哪一步?

from multiprocessing import Pool import tqdm import time def _foo(my_number): square = my_number * my_number time.sleep(1) return square if __name__ == '__main__': p = Pool(2) r = p.map(_foo, tqdm.tqdm(range(0, 30))) p.close() p.join()

欢迎任何帮助或建议...

Any help or suggestions are welcome...

推荐答案

发现的解决方案:注意!由于进行了多处理,估计时间(每个循环的迭代次数,总时间等)可能不稳定,但是进度条可以正常工作.

Solution Found : Be careful! Due to multiprocessing, estimation time (iteration per loop, total time, etc.) could be unstable, but the progress bar works perfectly.

注意:Pool的上下文管理器仅在Python 3.3版中可用

Note: Context manager for Pool is only available from Python version 3.3

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 enumerate(p.imap_unordered(_foo, range(0, max_))): pbar.update()

更多推荐

多重处理:使用tqdm显示进度条

本文发布于:2023-11-23 09:47:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度条   tqdm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!