I have a function which will increment a number with time. This is given to a process; but it doesn’t increment while the process runs. I want the incremented value of my global variable at regular intervals. But, I am not seeing the increment happening.
Here is what I tried:
<code>from multiprocessing import Process
from time import sleep
x = 0;
def increment():
global x;
while(1):
x +=1
sleep(1)
process_1 = Process(target=increment)
process_1.start()
for i in range(0,10):
print(x);
sleep(2)
</code>
<code>from multiprocessing import Process
from time import sleep
x = 0;
def increment():
global x;
while(1):
x +=1
sleep(1)
process_1 = Process(target=increment)
process_1.start()
for i in range(0,10):
print(x);
sleep(2)
</code>
from multiprocessing import Process
from time import sleep
x = 0;
def increment():
global x;
while(1):
x +=1
sleep(1)
process_1 = Process(target=increment)
process_1.start()
for i in range(0,10):
print(x);
sleep(2)
I am expecting that it will print
<code>1
3
5
7 ...
</code>
<code>1
3
5
7 ...
</code>
1
3
5
7 ...
However, it remains at 0.