BlueTrin
BlueTrin

Reputation: 10063

Bug in python/multiprocessing.pool?

(this is using Python 2.7)

I have found similar links but not about the exact same issue than what I am having. This program hangs on the map_async, and never finishes, I can see the Python process getting created but it never completes:

import multiprocessing


def main():
    PROCESSES = 4
    print 'Creating pool with %d processes\n' % PROCESSES
    pool = multiprocessing.Pool(PROCESSES)
    r = pool.map_async(pow3, range(10))
    r.wait()


def pow3(x):
    try:
        return x**3
    except:
        print('%s: %s' % (x, traceback.format_exc()))

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 1860

Answers (1)

Tal Weiss
Tal Weiss

Reputation: 8999

It works fine.
Are you running this in an interactive interpreter?
See this note from the docs:

Note Functionality within this package requires that the main module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter. For example:

>>> from multiprocessing import Pool   
>>> p = Pool(5)   

>>> def f(x):   
...     return x*x  

>>> p.map(f, [1,2,3])  

Process PoolWorker-1:
Process PoolWorker-2:
Process PoolWorker-3:

Traceback (most recent call last):

AttributeError: 'module' object has no attribute 'f'
AttributeError: 'module' object has no attribute 'f'
AttributeError: 'module' object has no attribute 'f'

(If you try this it will actually output three full tracebacks interleaved in a semi-random fashion, and then you may have to stop the master process somehow.)

Upvotes: 2

Related Questions