2

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?

HappyPy
  • 9,839
  • 13
  • 46
  • 68
  • `[pool.apply_async(foo, args=[n,L]) for n in l]` got `[, , , ]`, `print L` got `[[1, 2], [3, 4], [5, 6], [7, 8]]` `, – Gang Mar 22 '18 at 01:24
  • Multiprocessing works more dependably, on more systems, if you put the multiprocessing code in a 'main' clause, as in all examples in the doc. https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing – Terry Jan Reedy Mar 22 '18 at 01:24
  • It seems the issue was that there was an error in my code but no traceback printed. I've edited my question – HappyPy Mar 22 '18 at 20:31

1 Answers1

3

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)
Sraw
  • 18,892
  • 11
  • 54
  • 87
  • @Straw: It seems the issue was that there was an error in my code, but no traceback printed. I've edited my question – HappyPy Mar 22 '18 at 20:32
  • you can debug your code by `pool.apply(...)`. This will run your code in blocking mode, and traceback will be printed. – Sraw Mar 23 '18 at 02:33