首页 > 解决方案 > 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,以便我可以根据房间内的人数采取行动。我也尝试在函数内部创建新变量并在外部对它们进行添加,但是由于多处理,它们在函数外部不存在。我希望这是有道理的,请帮忙。

标签: pythonmultiprocessingpython-multiprocessing

解决方案


使用 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()

推荐阅读