简体   繁体   中英

Unexpected output when running tqdm inside multiprocessing.Process

I am trying to show a separate progress bar for each thread in my program, although I'm also okay with a progress bar that shows the total progress overall. Here is the code:

def cluster(indexes, process_n):
    for index in tqdm(indexes, position=process_n, nrows=N_CORES + 1, desc='Process ' + str(process_n)):
        # Do something

and

process = multiprocessing.Process(target=cluster, args=(indexes, process_n))
process.start()
processes.append(process)

for process in processes:
    process.join()

However, I appear to print extra static progress bars while the function is running. This does not happen when I run only one thread (and hence one progress bar.) As I intend to run this code on 32 threads, the output gets really messy. Here is an example of the terminal output when running 8 threads:

Process 1: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:09<00:00, 13.86it/s]
Process 0: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:09<00:00, 13.25it/s]
Process 7: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:10<00:00, 12.54it/s]
Process 6: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:10<00:00, 12.54it/s]
Process 2: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:10<00:00, 12.50it/s]
Process 5: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:10<00:00, 12.43it/s]
Process 4: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:10<00:00, 12.40it/s]
Process 3: 100%|███████████████████████████████████████████████████████████████████████████████| 127/127 [00:10<00:00, 12.17it/s]
Process 4:  98%|█████████████████████████████████████████████████████████████████████████████▏ | 124/127 [00:10<00:00, 15.24it/s]
Process 3:  99%|██████████████████████████████████████████████████████████████████████████████▍| 126/127 [00:10<00:00, 17.41it/s]


Process 6:  63%|██████████████████████████████████████████████████▍                             | 80/127 [00:06<00:04, 11.72it/s]

Please tell me how to fix this. And if the solution involves using something other than multiprocessing.Process then please link some sample code so I can understand how it works. Thanks!

You can manage your tqdm bars manually and use multiprocessing.Queue to signal that bar should update.

This example will create 4 processes that do the work and one process that updates the bars through the Queue:

import random
from tqdm import tqdm
from time import sleep
import multiprocessing

N = 4


def cluster(indexes, n_proc, q):
    for index in indexes:
        # do some work
        sleep(random.randint(1, 10) / 10)

        # update progress bar:
        q.put_nowait(n_proc)


def update_bar(q):
    bars = [tqdm(total=10, position=i, desc=f"Process {i}") for i in range(N)]

    while True:
        n = q.get()
        bars[n].update()


if __name__ == "__main__":
    q = multiprocessing.Queue()

    # daemon=True means the script doesn't wait for this process to end
    process = multiprocessing.Process(target=update_bar, args=(q,), daemon=True)
    process.start()

    processes = []
    for i in range(N):
        process = multiprocessing.Process(
            target=cluster, args=(range(10), i, q)
        )
        process.start()
        processes.append(process)

    for process in processes:
        process.join()

    print("\n" * N)

Prints:

Process 0: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:06<00:00,  1.50it/s]
Process 1: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:05<00:00,  1.71it/s]
Process 2: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:04<00:00,  1.63it/s]
Process 3: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:05<00:00,  1.97it/s]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM