Posts

Showing posts with the label Multiprocessing

Appending To The Same List From Different Processes Using Multiprocessing

Answer : Global variables are not shared between processes. You need to use multiprocessing.Manager.list : from multiprocessing import Process, Manager def dothing(L, i): # the managed list `L` passed explicitly. L.append("anything") if __name__ == "__main__": with Manager() as manager: L = manager.list() # <-- can be shared between processes. processes = [] for i in range(5): p = Process(target=dothing, args=(L,i)) # Passing the list p.start() processes.append(p) for p in processes: p.join() print L See Sharing state between processes¶ ( Server process part). Falsetru's answer worked. But still, the list was not accessible beyond the with Manager() as manager: two changes were needed: adding L = [] in front of the if __name__ == "__main__": statement. Must be added as for some reason the last print(L) (the one outside of if ) is ex...