どのように私は、プログレスバーがファイルごとに表示され、反復は、各ループにするには、このコードを変更することができますか?

DarkDrassher34:

私は取得に苦しんでいますtqdm新しい行への書き込みとは反対に滞在し、更新するのプログレスバーを。注:私は使用していますmultiprocessing私のコードを並列化するために、そしてtqdm私が並列しています関数内です。

私は、追加printのプログラムを実行するときに、ファイルはすべて私の端末に表示されますので、文を。以下の再現性の例:

import multiprocessing
import time

from tqdm import tqdm
from joblib import Parallel, delayed


def run_file_analysis(text):
    cool = []
    for i in tqdm(range(0, 10), position = 0, leave = True, desc = f'Text : {text}'):
        print('')
        cool.append(i)
        time.sleep(1)

num_cores = multiprocessing.cpu_count()
ls = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

if __name__ == "__main__":
    processed_list = Parallel(n_jobs=num_cores)(delayed(run_file_analysis)(i) for i in ls)

電流出力: ここでは、画像の説明を入力します。

The desired output would be the ten text objects - 1, 2, 3, ... , 10 and a corresponding updating progress bar for each. Not 100 different ones. I have tried following many stackoverflow questions relating to the topic of tqdm and multiprocessing integration, but none of them are as straightforward as I would like them to be. Any help would be appreciated.

Querenker :

As already discussed in the comments, you don't want to add an extra new line with the print statement. Instead you want to use the position argument in tqdm. The use case for different threads is even mentioned in the docs.

position : int, optional
  Specify the line offset to print this bar (starting from 0)
  Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads).

現在、この引数は0に設定されているので、毎回、新しいプログレスバーを開始します。代わりに、スレッドの数を使用します。そのため単純で、あなたは整数に指定されたテキストに変換し、これを使用することができます。しかし、これは生産のために推奨されません。

import multiprocessing
import time

from tqdm import tqdm
from joblib import Parallel, delayed


def run_file_analysis(text):
    cool = []
    for i in tqdm(range(0, 10), position=int(text), leave=True, desc = f'Text : {text}'):
        cool.append(i)
        time.sleep(1)

num_cores = multiprocessing.cpu_count()
ls = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

if __name__ == "__main__":
    processed_list = Parallel(n_jobs=num_cores)(delayed(run_file_analysis)(i) for i in ls)

テキストのを直接整数に変換できない場合は、「列挙」は指数関数に渡すことができます使用することができます。

import multiprocessing
import time

from tqdm import tqdm
from joblib import Parallel, delayed


def run_file_analysis(text, job_number):
    cool = []
    for i in tqdm(range(0, 10), position=job_number, leave=True, desc = f'Text : {text}'):
        cool.append(i)
        time.sleep(1)

num_cores = multiprocessing.cpu_count()
ls = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

if __name__ == "__main__":
    processed_list = Parallel(n_jobs=num_cores)(delayed(run_file_analysis)(text, i) for i, text in enumerate(ls))

編集:

いくつかのraceconditionsを設定することにより、減少させることができるprefer='threads'パラレルコンストラクタに:

if __name__ == "__main__":
    processed_list = Parallel(n_jobs=num_cores, prefer="threads")(delayed(run_file_analysis)(text, i) for i, text in enumerate(ls))

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=282829&siteId=1