python - pool.map_async 的打印进度

标签 python multiprocessing

我有以下功能

from multiprocessing import Pool
def do_comparison(tupl):
    x, y = tupl # unpack arguments
    return compare_clusters(x, y)

def distance_matrix(clusters, condensed=False):
    pool = Pool()
    values = pool.map_async(do_comparison, itertools.combinations(clusters, 2)).get()
    do stuff

是否可以打印pool.map_async(do_comparison, itertools.combinations(clusters, 2)).get()的进度? 我通过像这样向 do_comparison 添加计数来尝试

count = 0
def do_comparison(tupl):
    global count
    count += 1
    if count % 1000 == 0:
        print count
    x, y = tupl # unpack arguments
    return compare_clusters(x, y)

但除了它看起来不是一个好的解决方案之外,数字直到脚本结束才会打印。有什么好的方法吗?

最佳答案

我按如下方式跟踪进度:

import multiprocessing
import time

class PoolProgress:
  def __init__(self,pool,update_interval=3):
    self.pool            = pool
    self.update_interval = update_interval
  def track(self, job):
    task = self.pool._cache[job._job]
    while task._number_left>0:
      print("Tasks remaining = {0}".format(task._number_left*task._chunksize))
      time.sleep(self.update_interval)


def hi(x): #This must be defined before `p` if we are to use in the interpreter
  time.sleep(x//2)
  return x

a = list(range(50))

p   = multiprocessing.Pool()
pp  = PoolProgress(p)

res = p.map_async(hi,a)

pp.track(res)

关于python - pool.map_async 的打印进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19562916/

相关文章:

python - 为什么在尝试使用 SharedMemoryManager (python 3.8) 替代 BaseManager 时会出现 NameError 错误?

python - 多处理与 NumPy 不兼容

python - 如何在Python中对函数进行线程化

python - multiprocessing.Pool.imap_unordered 在 Python 2.6 中挂起?

python - 使用字典来减少元组列表

python - 如何提高 Sklearn GMM predict() 性能速度?

multithreading - 超线程处理器内核可以完全同时执行两个线程吗?

python - Django 模板 : Translate include with variable

python - 自 Mac OS 10.11 El Capitan 以来具有系统完整性保护的 DYLD_LIBRARY_PATH 技巧的替代方法

python - 从行中的多个值计算一个值