gpt4 book ai didi

python - pool.map_async 的打印进度

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:59 55 4
gpt4 key购买 nike

我有以下功能

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/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com