Python3: How to use set_description with tqdm.contrib.concurrent process_map

Viewed 1123

I have been using process_map from tqdm.contrib.concurrent : https://tqdm.github.io/docs/contrib.concurrent/

How can I set a description with the progress bar which changes in every iteration?

I have tried with: (stripped out a lot of code to simplify it here...)

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)

But I get AttributeError: 'str' object has no attribute 'desc'

Is it because process_map from tqdm.contrib.concurrent cannot be mixed with set_description from tqdm.tqdm ?

1 Answers

EDIT: process_map accepts an additional list of keyword arguments which are directly passed on to tqdm. This means you can simply use an additional keyword argument desc as follows.

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

AFAIK, there's no easy way to do this (as of yet). However, process_map takes an optional parameter called tqdm_class which you can use to your advantage (docs).

You can create a custom class which inherits the default class tqdm.tqdm and set attributes accordingly.

import tqdm

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

You can then pass on this custom class to process_map

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