python - 多处理:每个任务的进度条

标签 python multiprocessing

我有一个实现迭代算法的类。由于执行需要一段时间,并且我需要对多次执行的结果进行平均,因此我决定使用多处理。问题是我希望每次执行都有一个进度条(或不太花哨的东西)。像这样的东西:

experiment 1 [##########] 60%
experiment 2 [#############] 70%
experiment 3 [###] 20%

我的类看起来像这样(请注意,我已经使用了进度条,并且我想将其保留在那里,以便在不并行化时继续工作):

from __future__ import division
from time import sleep

class Algo():

    def __init__(self, total_iters, arg1, arg2, name):
        self.total_iters = total_iters
        self.arg1 = arg1
        self.arg2 = arg2
        self.name = name

    def step(self, iteration):
            """
            One iteration of Algorithm
            """
            # Progress bar
            completed = 100*iteration/self.total_iters
            if completed == 0: print ""
            print '\r {2} [{0}] {1}%'.format('#'*(int(completed/5)), completed, self.name),

            # Do some stuff
            sleep(0.001)

    def run(self):
        for i in xrange(self.total_iters):
            self.step(i)

        #  Output the final result in unique file

这是我的尝试:

import multiprocessing as mp 

if __name__ == "__main__":

    algo1 = Algo(200, 0,0, "test1")
    pool = mp.Pool(processes=3)
    for i in xrange(3):
        pool.apply_async(algo1.run) # in real life run will be passed N arguments

    pool.close()
    pool.join()

有什么想法吗?

PS:我试图避免诅咒

最佳答案

在 python 3 中又快又脏,但你会明白的;)

import random
import time
import multiprocessing
import os
import collections


class Algo(multiprocessing.Process):
    def __init__(self, steps, name, status_queue):
        multiprocessing.Process.__init__(self)
        self.steps = steps
        self.name = name
        self.status_queue = status_queue

    def step(self, step):
            # Progress bar
            self.status_queue.put((self.name, (step+1.0)/self.steps))
            # Do some stuff
            time.sleep(0.1)

    def run(self):
        for i in range(self.steps):
            self.step(i)


def print_progress(progress):
    # Windows:
    os.system('cls') 
    for name, percent in progress.items():
        percent = int(percent * 100)
        bar = ('#' * int(percent/10)) + (' ' * (10 - int(percent/10)))
        print("{}: [{}] {}%".format(name, bar, percent))

if __name__ == "__main__":
    status = multiprocessing.Queue()
    progress = collections.OrderedDict()
    algos = [Algo(random.randrange(100, 200), "test" + str(i), status) for i in range(3)]

    for a in algos:
        a.start()

    while any([a.is_alive() for a in algos]):
        while not status.empty():
            name, percent = status.get()
            progress[name] = percent
            print_progress(progress)

        time.sleep(0.1)

关于python - 多处理:每个任务的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30209609/

相关文章:

python - mysql-python fetchrow 带来: TypeError: 'long' object is not callable

python - 如何像在Photoshop上一样使用python调整图像颜色直方图

python - 在 python 列表类型结构中合理存储 10 亿多个值

python - 打印到同一行而不是新行?

Python 多处理如何优雅地退出?

python - 从多处理 python 函数中提取输出

C OpenMP 并行 for 循环未在多个线程上运行

python - 在 SQLAlchemy Core 中执行多个独立语句?

Python 多处理需要更多时间

multithreading - 功能完成后杀死 node.js 工作人员