【问题标题】:Python3: How to use set_description with tqdm.contrib.concurrent process_mapPython3:如何将 set_description 与 tqdm.contrib.concurrent process_map 一起使用
【发布时间】:2020-03-28 13:04:53
【问题描述】:

我一直在使用来自 tqdm.contrib.concurrent 的 process_map:https://tqdm.github.io/docs/contrib.concurrent/

如何设置每次迭代都会变化的进度条的描述?

我已经尝试过:(这里去掉了很多代码来简化它......)

from tqdm.contrib.concurrent import process_map 
import tqdm

def myfunc(htmlfile):

    tqdm.tqdm.set_description(htmlfile)

    ### function contents go here

r = process_map(myfunc, mylist, max_workers=16)

但我得到AttributeError: 'str' object has no attribute 'desc'

是不是因为 tqdm.contrib.concurrent 的 process_map 不能与 tqdm.tqdm 的 set_description 混合?

【问题讨论】:

    标签: python parallel-processing tqdm


    【解决方案1】:

    编辑process_map 接受直接传递给 tqdm 的附加关键字参数列表。这意味着您可以简单地使用额外的关键字参数desc,如下所示。

    r = process_map(myfunc, mylist, max_workers=16, desc="My Description")
    

    AFAIK,没有简单的方法可以做到这一点(到目前为止)。但是,process_map 采用一个名为 tqdm_class 的可选参数,您可以使用它来发挥自己的优势 (docs)。

    您可以创建一个继承默认类tqdm.tqdm 的自定义类并相应地设置属性。

    import tqdm
    
    class my_tqdm(tqdm.tqdm):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.desc = "My Description"
    

    然后您可以将此自定义类传递给process_map

    r = process_map(myfunc, mylist, max_workers=16, tqdm_class=my_tqdm)
    

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2018-08-19
      • 2016-11-01
      • 2016-05-18
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多