0

I run a multipocessing script like this:

def intersterer(i):
    somestuff
    return x

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

I want know how I can see the progress of the work. I look around a shared counter, but its ugly and seems not telling the truth. (Perhaps a wrong code in it) Can someone help me with a great pythonic way?

EDIT: Like my exemple, I want to store all results in a list, answers at the question : Show the progress of a Python multiprocessing pool map call? doesn't help me for this. Thks

EDIT2:

Here is the soluced way. Thks every helpers!

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)),
Community
  • 1
  • 1
Alex Jud
  • 85
  • 1
  • 9
  • 1
    Does [this](https://stackoverflow.com/questions/5666576/show-the-progress-of-a-python-multiprocessing-pool-map-call#5666996) help? – Aereaux May 29 '15 at 18:18
  • like my edit said it, it does not help me much, but my answer is founded! Thks – Alex Jud May 31 '15 at 08:04

1 Answers1

2

Use imap_unordered instead of 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]

I'm being lazy, and using pathos.multiprocessing so I can work from the interpreter… but this should also work for standard multiprocessing if done from a file and in __main__. In this solution, you have to know that there will be 45 combinations, but otherwise you can't get the percent w/o knowing the final length.

Since you don't care about the order and only care about speed, I've used imap_unordered. If you do care about order, then use imap.

EDIT: Aha… this is a duplicate question.

Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • Thanks for your response. I have one other request.... I don't care about about order, but I need the result in a list. Your way doesn't give me an iterable object.... I don't find `uimap`... can you tell me more about? – Alex Jud May 29 '15 at 19:38
  • It's `imap_unordered` in the standard module. I'll edit my post to give you a list back. – Mike McKerns May 29 '15 at 20:33