[Python] How to create individual rich progress bars for each worker in Python...

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple code that you can run (the logging is to differentiate 4 workers):

    import time
    import random
    import logging
    import logging.handlers
    from multiprocessing.dummy import Pool

    def do_something(number):
    logger.info(number)
    time.sleep(number/100)

    logger = logging.getLogger(__name__)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)-7s] (%(threadName)-10s) %(message)s"))
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)

    numbers = random.sample(range(1, 101), 50)

    pool = Pool(4)
    pool.imap_unordered(do_something, numbers)
    pool.close()
    pool.join()


    How do I add an individual rich progress bar for each of the 4 workers? Ideally, I would like a 5th main progress bar that tracks the total completion, but if it's too much work, then it's not necessary.

    Something like this: [​IMG]

    With tqdm, it seems you can just do:

    for _ in tqdm.tqdm(pool.imap_unordered(do_something, numbers), total=len(numbers)):
    pass


    but I want to use rich since I like its customization better.

    Continue reading...

Compartilhe esta Página