HappyPy
HappyPy

Reputation: 10697

append to the same list with multiprocessing - python

I'd like different processes to append to the same list:

import multiprocessing as mp

def foo(n,L):
    a
    L.append(n)

pool = mp.Pool(processes=2)
manager = mp.Manager()

L= manager.list()

l=[[1,2],[3,4],[5,6],[7,8]]

[pool.apply_async(foo, args=[n,L]) for n in l]

However,

>> print L
[]

What am I doing wrong?

EDIT: The problem was that there was traceback in my original code which wasn't printed on the screen. For example if I run the code above and a doesn't exist, the rest of the code isn't evaluated, but there is no exception raised. Is there a way to make multiprocessing print tracebacks so that I can debug the code?

Upvotes: 2

Views: 6291

Answers (1)

Sraw
Sraw

Reputation: 20224

Because you haven't waited tasks to finish.

import multiprocessing as mp

def foo(n,L):
    L.append(n)

pool = mp.Pool(processes=2)
manager = mp.Manager()

L= manager.list()

l=[[1,2],[3,4],[5,6],[7,8]]

[pool.apply_async(foo, args=[n,L]) for n in l]
pool.close()
pool.join()
print(L)

Upvotes: 3

Related Questions