【问题标题】:Progress in a itertools.combinations pool() map , result passed in listitertools.combinations pool() 映射中的进度,结果在列表中传递
【发布时间】:2015-08-12 17:17:54
【问题描述】:

我像这样运行一个多进程脚本:

def intersterer(i):
    somestuff
    return x

if __name__ == '__main__':
    pool = Pool() 
    list = pool.map(intersterer, itertools.combinations(xrange(great_number), 2))  

我想知道如何查看工作的进度。我环顾了一个共享柜台,但它很丑,而且似乎没有说实话。 (可能里面有错误的代码) 有人可以用一种很棒的 Python 方式帮助我吗?

编辑: 就像我的例子一样,我想将所有结果存储在一个列表中,问题的答案:Show the progress of a Python multiprocessing pool map call? 对此没有帮助。谢谢

EDIT2:

这是解决方法。感谢每一位帮助者!

def intersterer(i):
    somestuff
    return x

numtot = binomial(number,2)
pool = Pool()   
list=[]
tpsb=time.time()
for i in pool.imap_unordered(intersterer, itertools.combinations(xrange(number),2)):
    list.append(i); print "{:3.2f}%, approximatly {:3.2f} seconds left       \r".format(100.*len(list)/numtot,  (time.time()-tpsb)*100./(100.*len(list)/numtot)-(time.time()-tpsb)),

【问题讨论】:

  • this 有帮助吗?
  • 就像我的编辑说的那样,它对我没有多大帮助,但我的答案是有根据的!谢谢

标签: python multiprocessing


【解决方案1】:

使用imap_unordered 代替map

>>> from pathos.multiprocessing import Pool
>>> pool = Pool() 
>>> import itertools
>>> 
>>> def interstellar(i):
...   return sum(i)**2
... 
>>> result = []
>>> for i in pool.imap_unordered(interstellar, itertools.combinations(xrange(10),2)):
...   result.append(i); print "{:3.2f}%".format(100.*len(result)/45)
... 
2.22%
4.44%
6.67%
8.89%
11.11%
13.33%
15.56%
17.78%
20.00%
22.22%
24.44%
26.67%
28.89%
31.11%
33.33%
35.56%
37.78%
40.00%
42.22%
44.44%
46.67%
48.89%
51.11%
53.33%
55.56%
57.78%
60.00%
62.22%
64.44%
66.67%
68.89%
71.11%
73.33%
75.56%
77.78%
80.00%
82.22%
84.44%
86.67%
88.89%
91.11%
93.33%
95.56%
97.78%
100.00%
>>> result
[1, 4, 9, 16, 25, 36, 49, 64, 81, 9, 16, 25, 36, 49, 64, 81, 100, 25, 36, 49, 64, 81, 100, 121, 49, 64, 81, 100, 121, 144, 81, 100, 121, 144, 169, 121, 144, 169, 196, 169, 196, 225, 225, 256, 289]

我很懒,使用pathos.multiprocessing,所以我可以在解释器中工作……但如果从文件和__main__ 中完成,这也应该适用于标准multiprocessing。在此解决方案中,您必须知道会有 45 组合,否则您无法在不知道最终长度的情况下获得百分比。

由于你不关心顺序,只关心速度,所以我使用了imap_unordered。如果您确实关心订单,请使用imap

编辑:啊哈……这是一个重复的问题。

【讨论】:

  • 感谢您的回复。我还有另一个要求....我不在乎订单,但我需要列表中的结果。你的方式没有给我一个可迭代的对象......我找不到uimap......你能告诉我更多吗?
  • 在标准模块中是imap_unordered。我会编辑我的帖子给你一个列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 2020-04-10
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多