我使用2个python进程,我想知道如何共享和更新变量。 我设法将变量发送到流程,但在此过程中不会更新此变量。
在我的代码中,当启动进程worker
时,它每3秒增加一次变量a
。
与此同时,流程my_service
会持续显示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()
但结果并非我所期望的:
worker= 1
worker= 1
my_service= 8
my_service= 8
worker= 2
worker= 2
my_service= 8
worker= 3
worker= 3
my_service= 8
变为worker
的变量增加了,但变量未在过程service
那么如何在进程之间共享更新的变量?
THX,
答案 0 :(得分:1)
使用python进行多处理的问题在于,任何进程都与其他进程完全独立。在lauching上,它复制当前变量然后处理此副本:这意味着对变量状态的任何修改都不会复制到另一个进程。 这是由Python的全局解释器锁引起的,它确保只有一个进程可以同时访问变量,以避免破坏内存。您可以在此处查看更多内容:What is a global interpreter lock (GIL)?
现在针对您的特定问题,您可以使用共享变量。
from multiprocessing import Value
a=Value('f', 0.0) # create a shared float, initialised at 0
a.value # read the value
a.value=50 # modify the value
您需要声明a
并将其作为每个流程的参数传递。
但是当你"绕过"在GIL中,您需要管理自己对此变量的访问,以避免让2个进程同时尝试读取/修改它。这就是为什么每个共享变量都带有Lock
,允许访问变量。
a.acquire() #acquire the Lock, forbidding access to other processes.
a.value # read the value
a.value=50 # modify the value
a.release() # don't forget to release the lock, or else you will block everything.
请注意,如果出现错误/异常,如果锁定未被释放,则对您的变量的访问将永久丢失。如果这是一个问题,请添加:
try:
a.acquire() #acquire the Lock, forbidding access to other processes.
a.value # read the value
a.value=50 # modify the value
a.release() # don't forget to release the lock, or else you will block everything.
except Exception as e:
print e
a.release()
您的最终代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import multiprocessing
from multiprocessing import Value
import time
#from globalvar import *
a=Value('f', 8)
#toto=8
def worker(a):
try:
name = multiprocessing.current_process().name
for i in range(1,4):
a.acquire()
a.value=i
a.release()
print ("worker=",a.value)
time.sleep(3)
except Exception as e:
print e
a.release()
def my_service(az):
name = multiprocessing.current_process().name
# print (name,"Starting")
# time.sleep(3)
# print (name, "Exiting")
while True:
try:
az.acquire()
print ("my_service=",az.value)
az.release()
time.sleep(2)
except Exception as e:
print e
az.release()
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,args=(a,))
worker_2 = multiprocessing.Process(target=worker,args=(a,)) # use default name
worker_1.start()
worker_2.start()
service.start()