0

I've got two functions that I call in a thread each passing in the same dictionary that they add to. I also have a third function that runs outside of the thread after both the threaded functions finish that also adds items into the dictionary.

When I check the contents of the dictionary, it only contains the contents of the third function. An example of what I'm talking about is below:

from multiprocessing import Process, Pipe

my_dict = {}

process1 = Process(target=class1.run, args=(my_dict,))
process2 = Process(target=class2.run, args=(my_dict,))

process1.start()
process2.start()

process1.join()
process2.join()

class3.run(my_dict)

print(my_dict)

What's the workaround to allow me to pass the dictionary to each function (threaded or not) and ensure all values are inserted?

IonicSolutions
  • 2,559
  • 1
  • 18
  • 31
mlevit
  • 2,676
  • 10
  • 42
  • 50
  • The answers to [this related question](https://stackoverflow.com/questions/10415028/how-can-i-recover-the-return-value-of-a-function-passed-to-multiprocessing-proce) might help you – IonicSolutions Apr 18 '19 at 10:47

1 Answers1

2

You're using multiprocessing. It spins up new Processes for each of the functions that you're calling. Unlike, threading, processes don't hare a memory space. Each of the two processes you're spinning up will get a copy of my_dict. Hence the modifications made to my_dict from process1 or proces2 will not not be visible to the my_dict in class3 (since they are different objects in different memory spaces)

If you want to share information between processes you can use Queues - let each Process send the modified dict back in a queue message, and then merge then before passing it to class3. https://docs.python.org/2/library/multiprocessing.html#pipes-and-queues

Or if you actually meant to use threads you need to switch to using the threading module. https://docs.python.org/2/library/thread.html (Be aware of the GIL in this case)

rdas
  • 20,604
  • 6
  • 33
  • 46
  • That's the one mate... thanks very much man. I changed it to threading instead and it worked. – mlevit Apr 18 '19 at 10:53