gpt4 book ai didi

python - 将变量更新为多处理 python

转载 作者:太空宇宙 更新时间:2023-11-03 17:08:45 26 4
gpt4 key购买 nike

我使用 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 中未显示更新

那么如何在进程之间共享更新的变量呢?

谢谢,

最佳答案

Python 多处理的问题是每个进程都完全独立于其他进程。启动时,它会复制当前变量,然后处理此副本:这意味着对变量状态的任何修改都不会复制到其他进程。这是由 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()

关于python - 将变量更新为多处理 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34337514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com