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!

jason m
  • 6,081
  • 19
  • 65
  • 117

1 Answers1

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')
Andrej Kesely
  • 118,151
  • 13
  • 38
  • 75
  • 7
    This actually also hangs – jason m Jun 26 '19 at 20:12
  • @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. – Andrej Kesely Jun 26 '19 at 20:16
  • 1
    Yes running the same code. It does not work and the context does not work in py27 as indicated. https://stackoverflow.com/questions/25968518/python-multiprocessing-lib-error-attributeerror-exit – jason m Jun 26 '19 at 20:50
  • My question was poorly phrased and your solution indeed works. – jason m Jun 26 '19 at 22:43