I have been trying to pass data into a loop in another process by creating an empty object in which I put the data in. When I change the content of my object the process doesn't seem to update, and keeps returning the same value.
Here is some code I have tried:
from multiprocessing import Process
from time import sleep
class Carrier(): # Empty object to exchange data
pass
def test():
global carry
while True:
print(carry.content)
sleep(0.5)
carry = Carrier()
carry.content = "test" # Giving the value I want to share
p = Process(target=test, args=())
p.start()
while True:
carrier.content = input() # Change the value from the object
I have also tried deleting the object from memory each time and redefining it in the next loop but doesn't seem to have any effect, instead it keeps the initial "test" value, the one present when it was executed.
1 Answer 1
I assuming that you are running this program on windows as the child process or threads you open in a program are independent of their parent process. So you actually can't share data between the process like this.
It is possible to share data in when running python on linux as the spawned processes are child of parent process where it can share the data.
To be able to share data between parent and child process irrespective of the underlying OS, you can see this link - How to share data between Python processes?
Comments
Explore related questions
See similar questions with these tags.
carryvariable is defined afterdef test(). Theglobal carrystatement cannot find acarryvariable at the time whendef test()is being defined.Processlike that.