将tqdm与多进程结合使用以处理多个进度条

时间:2019-03-04 14:43:18

标签: python python-multiprocessing concurrent.futures tqdm process-pool

我想监视跨不同进程的多个工作人员的进度。对于每个子流程,我都有自己的进度条,但是它不能与ProcessPoolExecutor执行程序一起正常工作。

def main():
    with futures.ProcessPoolExecutor(max_workers=PROCESSES) as executor:
        fut_to_num = {}
        for i in range(PROCESSES):
            fut = executor.submit(execute_many_threads, i)
            fut_to_num[fut] = i

        for future in futures.as_completed(fut_to_num):
            r = future.result()
            # print('{} returned {}'.format(fut_to_num[future], r))
    print('\nDone!\n')


def execute_many_threads(n_pool=0):
    with futures.ThreadPoolExecutor(max_workers=THREADS) as executor:
        for i in range(THREADS):
            executor.submit(execute_thread, n_pool, i)
    return n_pool+1


def execute_thread(n_pool=0, n_thread=0):
    s = random.randint(1, 5)
    thread_num = n_pool*(PROCESSES-1) + n_thread

    progress = tqdm.tqdm(
        desc='#{:02d}'.format(thread_num),
        position=thread_num,
        total=10*s,
        leave=False,
    )
    # print('Executing {}: {}...'.format(thread_num, s))
    for i in range(s):
        time.sleep(1)
        progress.update(n=10)
    progress.close()
    return s

当用ThreadPoolExecutor(第二行)替换时,一切正常,因此我认为这可能与某些序列化问题有关。使用多处理程序时,能否帮助您找出tqdm的正确调用。

python --versionPython 3.7.2tqdm==4.31.1Ubuntu 18.04

0 个答案:

没有答案