1

I try to use simple way to make mulitiprocessing:

urls = [1, 2, 3, 4]
p = Pool(2)
p.map(open, urls)

Where open() is function, that does calculations.

def open(url):
    print(url)

When I do print(url) it returns me strange result:

1
2
3
4
1
2
3
4

I can assume that each of process Pool(2) handler the same calculations there is wrong.

I need that process 1 takes [1, 2] for handling and process 2 takes [3, 4]

Babajaha
  • 11
  • 1
  • you may check this: http://stackoverflow.com/questions/26520781/multiprocessing-pool-whats-the-difference-between-map-async-and-imap. Maybe `map` is too synchronous for you. – Jean-François Fabre Dec 21 '16 at 20:39
  • Don't use `open` as a function name, it's a builtin. Also, are you on Windows? – roganjosh Dec 21 '16 at 20:40
  • Check out this thread: http://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments – Peter234 Dec 21 '16 at 20:41
  • @Peter234 there aren't multiple arguments though? It's meant to take an iterable, which `urls` is. – roganjosh Dec 21 '16 at 20:42
  • You're not joining or closing the pool in this code. Is this an accurate representation of your approach? Granted I'm on Windows so I've had to shield with `if __name__ == '__main__':` but currently I get no printouts at all, and zombie processes. – roganjosh Dec 21 '16 at 20:45
  • No, I run script on Cent OS – Huligan Dec 21 '16 at 21:46

1 Answers1

0

I think this should do what you want to:

import multiprocessing
def func(x):
    return x[0] + x[1]

if __name__ == "__main__":    
    p = multiprocessing.Pool(2)
    for x in p.map(func, [(1, 2), (3, 4)]):
        print(x)

output:

 3
 7
Peter234
  • 1,052
  • 7
  • 24