3

I have a very simple script which is the following:

import multiprocessing as multi def call_other_thing_with_multi(): P = multi.Pool(3) P.map(other_thing, range(0,5)) P.join() def other_thing(arg): print(arg) return arg**2. call_other_thing_with_multi() 

When I call this, my code hangs at perpetuity. This is on windows with python 2.7.

Thanks for any guidance!

1 Answer 1

2

As per documentation, you need to call close() before join():

import multiprocessing as multi def call_other_thing_with_multi(): P = multi.Pool(3) P.map(other_thing, range(0,5)) P.close() # <-- calling close before P.join() P.join() print('END') def other_thing(arg): print(arg) return arg**2. call_other_thing_with_multi() 

Prints:

0 1 2 3 4 END 

EDIT: Better is use context manager, to not forget to call close():

def call_other_thing_with_multi(): with multi.Pool(3) as P: P.map(other_thing, range(0,5)) print('END') 
Sign up to request clarification or add additional context in comments.

4 Comments

This actually also hangs
@jasonm Are you running exactly the same code? I'm on Python 3.6, but it shouldn't be a difference. I added version with context manager.
Yes running the same code. It does not work and the context does not work in py27 as indicated. stackoverflow.com/questions/25968518/…
My question was poorly phrased and your solution indeed works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.