相关疑难解决方法(0)

570
推荐指数
5
解决办法
24万
查看次数

多处理:使用tqdm显示进度条

为了使我的代码更"pythonic"和更快,我使用"多处理"和一个map函数发送它a)函数和b)迭代范围.

植入的解决方案(即直接在范围tqdm.tqdm(范围(0,30))上调用tqdm不适用于多处理(如下面的代码所示).

进度条显示从0到100%(当python读取代码?)但它不指示map函数的实际进度.

如何显示一个进度条,指示"地图"功能在哪一步?

from multiprocessing import Pool
import tqdm
import time

def _foo(my_number):
   square = my_number * my_number
   time.sleep(1)
   return square 

if __name__ == '__main__':
   p = Pool(2)
   r = p.map(_foo, tqdm.tqdm(range(0, 30)))
   p.close()
   p.join()
Run Code Online (Sandbox Code Playgroud)

欢迎任何帮助或建议......

python multiprocessing progress-bar tqdm

67
推荐指数
7
解决办法
4万
查看次数

将 tqdm 与多重处理结合使用以实现多个进度条

我想监控不同进程的多个工作人员的进度。对于每个子进程,我都有自己的进度条,但它不能与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): …
Run Code Online (Sandbox Code Playgroud)

python concurrent.futures python-multiprocessing tqdm process-pool

7
推荐指数
0
解决办法
6045
查看次数

如何在多线程中使用 tqdm?

我正在尝试使用 tqdm 报告从三个链接下载每个文件的进度,我想使用多线程从每个链接同时下载同时更新进度条。但是当我执行我的脚本时,有多行进度条似乎线程正在同时更新 tqdm 进度条。我在问我应该如何运行多线程来下载文件,同时保持每次下载的进度条,而不会有重复的条填充整个屏幕?这是我的代码。

import os
import sys
import requests
from pathlib import Path
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor as PE


def get_filename(url):
    filename = os.path.basename(url)
    fname, extension = os.path.splitext(filename)
    if extension:
        return filename
    header = requests.head(url).headers
    if "Location" in header:
        return os.path.basename(header["Location"])
    return fname


def get_file_size(url):
    header = requests.head(url).headers
    if "Content-Length" in header and header["Content-Length"] != 0:
        return int(header["Content-Length"])
    elif "Location" in header and "status" not in header:
        redirect_link = header["Location"]
        r = requests.head(redirect_link).headers
        return …
Run Code Online (Sandbox Code Playgroud)

python multithreading tqdm

3
推荐指数
1
解决办法
4148
查看次数