如何在命令行中为池进程创建进度条?

2022-04-10 00:00:00 python python-multiprocessing

问题描述

我有几个使用多处理池运行的脚本,我正在尝试根据完成的脚本做一个进度条。

我检查了https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console#=

但我想不出如何组合在柜台中完成的脚本

import os                                                                       
from multiprocessing import Pool

def run_process(process):                                                             
    os.system('python {}'.format(process))

processes = ('script1.py', 'script2.py','script3.py','script4.py')

if __name__ == "__main__":

    pool = Pool(processes=2)
    pool.map(run_process, processes)

解决方案

这里有一个略有不同的方法,它使用concurrent.futures.ThreadPoolExecutor而不是multiprocessing.Pool,这使它比other answer中的内容更简单、更高效。

注意它使用与我的其他答案中相同的print_progress_bar.py模块。

import concurrent.futures
import os
import subprocess
import sys

from print_progress_bar import print_progress_bar
progress_bar_kwargs = dict(prefix='Progress:', suffix='Complete', length=40)

# To simplify testing just using one script multiple times.
processes = ('./mp_scripts/script1.py', './mp_scripts/script1.py',
             './mp_scripts/script1.py', './mp_scripts/script1.py')
process_count = 0


def run_process(process):
    global process_count

    subprocess.run([sys.executable,  process])
    # Update process count and progress bar when it's done.
    process_count += 1
    print_progress_bar(process_count, len(processes), **progress_bar_kwargs)


print_progress_bar(0, len(processes), **progress_bar_kwargs) # Print 0% progress.
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:

    future_to_process = {executor.submit(run_process, process): process
                            for process in processes}
    for future in concurrent.futures.as_completed(future_to_process):
        process = future_to_process[future]
        try:
            _ = future.result()
        except Exception as exc:
            print()
            print(f'{process} generated an exception: {exc}')

    print('Done')

相关文章