Python 多处理变量

Python Multiprocessing Variable

我基本上需要全球化变量DR

我创建了一个名为 DR 的变量,并在函数外将其设置为 0。然后函数(确定是否有东西进入或离开房间)在有人进入时将 DR 加 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,以便我可以根据房间内的人数采取行动。我也尝试在函数内部创建新变量并在外部对它们进行添加,但是,由于多处理,它们在函数外部不存在。 我希望这是有道理的,请帮忙。

使用 multiprocessing.Value 作为函数的参数,并使用 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()