python - Python 多处理变量
问题描述
我基本上需要将变量 DR 全球化
我创建了一个名为 DR 的变量,并在函数之外将其设置为 0。然后函数(确定是否有东西进入或离开房间)当有人进入时将 1 加到 DR 上,当有人离开时将 1 带走。
from multiprocessing import Process, Value
import multiprocessing
DR=0
def loop_out():
global DR
while True:
# Read the sensor data
light2_level = ReadChannel(light2_channel)
light1_level = ReadChannel(light1_channel)
# Print out results
if light1_level>650:
#print("Light1: {} ({}V)".format(light1_level))
if light2_level>650:
print ("---GOING OUT---")
DR-=1
print(DR)
time.sleep(1)
def loop_in():
global DR
while True:
# Read the sensor data
light2_level = ReadChannel(light2_channel)
light1_level = ReadChannel(light1_channel)
if light2_level>650:
#print("Light2 : {} ({}V)".format(light2_level))
if light1_level>650:
print("---GOING IN---")
DR+=1
print(DR)
time.sleep(1)
#This next part executes the two functions in multi-processing
if __name__ == '__main__':
p1=Process(target=loop_out)
p1.start()
p2=Process(target=loop_in)
p2.start()
问题是,函数外部的 DR 值保持为 0,而“in”函数内部随着人的进入而继续增加,而“out”函数内部随着人们离开而继续减少,因此您会收到以下输出:
*someone enters"
GOING IN
People in room: 1
*someone enters"
GOING IN
People in room: 2
*someone leaves"
GOING OuT
People in room: -1
*someone enters"
GOING IN
People in room: 3
*someone enters"
GOING OuT
People in room: -2
我需要在全局范围内更改 DR,以便我可以根据房间内的人数采取行动。我也尝试在函数内部创建新变量并在外部对它们进行添加,但是由于多处理,它们在函数外部不存在。我希望这是有道理的,请帮忙。
解决方案
使用 amultiprocessing.Value
作为函数的参数,并使用 a 保护它,multiprocessing.Lock
因为它是共享资源:
import multiprocessing
import time
# BEGIN MOCK
light1_channel = None
light2_channel = None
def ReadChannel(channel):
from random import randint
return randint(600, 700)
# END MOCK
def loop_out(DR, lock):
while True:
# Read the sensor data
light2_level = ReadChannel(light2_channel)
light1_level = ReadChannel(light1_channel)
# Print out results
if light1_level > 650:
# print("Light1: {} ({}V)".format(light1_level))
if light2_level > 650:
print ("---GOING OUT---")
lock.acquire()
DR.value -= 1
lock.release()
print(DR.value)
time.sleep(1)
def loop_in(DR, lock):
while True:
# Read the sensor data
light2_level = ReadChannel(light2_channel)
light1_level = ReadChannel(light1_channel)
if light2_level > 650:
# print("Light2 : {} ({}V)".format(light2_level))
if light1_level > 650:
print("---GOING IN---")
lock.acquire()
DR.value += 1
lock.release()
print(DR.value)
time.sleep(1)
# This next part executes the two functions in multi-processing
if __name__ == '__main__':
DR = multiprocessing.Value('i', 0)
lock = multiprocessing.Lock()
p1 = multiprocessing.Process(target=loop_out, args=(DR, lock))
p1.start()
p2 = multiprocessing.Process(target=loop_in, args=(DR, lock))
p2.start()
编辑:这是另一个没有Lock
实例的版本,因为正如 Miyagi 先生在评论中提到的那样,该Value
实例已经默认包含一个锁:
import multiprocessing
import time
# BEGIN MOCK
light1_channel = None
light2_channel = None
def ReadChannel(channel):
from random import randint
return randint(600, 700)
# END MOCK
def loop_out(DR):
while True:
# Read the sensor data
light2_level = ReadChannel(light2_channel)
light1_level = ReadChannel(light1_channel)
# Print out results
if light1_level > 650:
# print("Light1: {} ({}V)".format(light1_level))
if light2_level > 650:
print ("---GOING OUT---")
with DR.get_lock():
DR.value -= 1
print(DR.value)
time.sleep(1)
def loop_in(DR):
while True:
# Read the sensor data
light2_level = ReadChannel(light2_channel)
light1_level = ReadChannel(light1_channel)
if light2_level > 650:
# print("Light2 : {} ({}V)".format(light2_level))
if light1_level > 650:
print("---GOING IN---")
with DR.get_lock():
DR.value += 1
print(DR.value)
time.sleep(1)
# This next part executes the two functions in multi-processing
if __name__ == '__main__':
DR = multiprocessing.Value('i', 0)
p1 = multiprocessing.Process(target=loop_out, args=(DR,))
p1.start()
p2 = multiprocessing.Process(target=loop_in, args=(DR,))
p2.start()
推荐阅读
- amazon-web-services - AWS S3 - Golang 开发工具包 - SignatureDoesNotMatch
- html - 标签的href在悬停时显示不同的url
- python-3.x - 如何使用 ProcessPool 在进程之间有效地共享字典和列表
- reactjs - 无法从连接的 React 组件触发 createAsyncThunk
- javascript - 删除一个对象的键也会删除另一个对象中的键 - Javascript
- swiftui - 从 UIViewControllerRepresentable 对象访问 @Environment 对象
- javascript - React native - “渲染的钩子比之前的渲染更多?”
- r - 使用 ggarrange,地块的标题没有出现
- .net-core - Blazor WebAssembly 3.2.0 构建失败
- java - JavaFX - 由于画布清除/重绘,调整简单应用程序的大小会导致“闪烁”