I use 2 python process and i wonder how share and update a variable. I manage to send variable to a process but this variable isn't updated during the process.
In my code, when the process worker
is launched, it increase the variable a
every 3 sec.
In the same time the process my_service
show continuously the value of a
.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#import multiprocessing as mp
#from multiprocessing import Process
import multiprocessing
import time
from globalvar import *
a=8
#toto=8
def worker():
name = multiprocessing.current_process().name
# print (name,"Starting")
# time.sleep(2)
# print (name, "Exiting")
for a in range(1,4):
print ("worker=",a)
time.sleep(3)
def my_service(az):
name = multiprocessing.current_process().name
# print (name,"Starting")
# time.sleep(3)
# print (name, "Exiting")
while True:
print ("my_service=",az)
time.sleep(2)
if __name__ == '__main__':
#Process(target=worker).start()
service = multiprocessing.Process(name='my_service', target=my_service,args=(a,))
worker_1 = multiprocessing.Process(name='worker 1', target=worker)
worker_2 = multiprocessing.Process(target=worker) # use default name
worker_1.start()
worker_2.start()
service.start()
But the result isn't what i expect:
worker= 1
worker= 1
my_service= 8
my_service= 8
worker= 2
worker= 2
my_service= 8
worker= 3
worker= 3
my_service= 8
The variable into worker
is increased, but the variable isn't shown uptaded in process service
So how to share updated varaible between process?
Thx,