I'm trying to set a global variable in one process and read it from another process. This is basically what i am doing:
from multiprocessing import Process
import time
rocket = 0
def func1():
global rocket
while rocket < 10:
rocket += 1
print("Func1: " + str(rocket))
time.sleep(5)
def func2():
while rocket < 10:
print ("Func2: " + str(rocket))
time.sleep(1)
if __name__=='__main__':
p1 = Process(target = func1)
p1.start()
p2 = Process(target = func2)
p2.start()
What I think this code should be doing:
- func1 increases the global variable 'rockets' by 1 every five seconds
- Every second func2 reads the global variable rockets and prints it
- The two methods run parralel until 'rockets' == 10
So the expected output should be something like:
Func1: 1
Func2: 1
Func2: 1
Func2: 1
Func2: 1
Func2: 1
Func1: 2
Func2: 2
Func2: 2
#... and so on
But the actual output goes like:
Func1: 1
Func2: 0
Func2: 0
Func2: 0
Func2: 0
Func2: 0
Func1: 2
Func2: 0
Func2: 0
#... and so on
When printed from func2 'rockets' always stays 0
I am declaring 'rockets' as a global variable in func1 as you should
What am I missing here?