I need to append objects to one list \"L\" from different processes using multiprocessing , but it returns empty list. How can i let many processes append to list \"L\"usi
Thanks to @falsetru for suggesting the exact documentation and providing the good code. I need to keep the order for my application and by modifying the @falsetru code, now the below code preserves the order of adding items to the list.
The sleep is helpful to catch the bugs otherwise it is hard to catch the problem with ordering of the list.
from multiprocessing import Process, Manager
from time import sleep
def dothing(L, i): # the managed list `L` passed explicitly.
L[i]= i
sleep(4)
if __name__ == "__main__":
with Manager() as manager:
L = manager.list(range(50)) # <-- can be shared between processes.
processes = []
for i in range(50):
p = Process(target=dothing, args=(L,i)) # Passing the list
p.start()
processes.append(p)
for p in processes:
p.join()
print(L)