簡體   English   中英

在 multiprocessing.Process 中運行 tqdm 時出現意外輸出

[英]Unexpected output when running tqdm inside multiprocessing.Process

我試圖為我的程序中的每個線程顯示一個單獨的進度條,盡管我也可以使用一個顯示總體總進度的進度條。 這是代碼:

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

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

for process in processes:
    process.join()

但是,我似乎在函數運行時打印了額外的靜態進度條。 當我只運行一個線程(因此只有一個進度條)時不會發生這種情況。因為我打算在 32 個線程上運行這段代碼,所以輸出變得非常混亂。 以下是運行 8 個線程時的終端輸出示例:

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]

請告訴我如何解決這個問題。 如果解決方案涉及使用 multiprocessing.Process 以外的其他東西,那么請鏈接一些示例代碼,以便我了解它是如何工作的。 謝謝!

您可以手動管理您的tqdm條並使用multiprocessing.Queue來表示條應該更新。

此示例將創建 4 個執行工作的進程和一個通過隊列更新條形的進程:

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)

印刷:

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM