python 进程之间共享内存

'''
共享内存
multiprocess组件Lock
'''
import multiprocessing
import time
from multiprocessing import Value, Manager

'''
Array数组
Manager
'''

import random


def add1(value, number):
    while True:
        number.value += value
        print("number add1 = {0}".format(number.value))
        time.sleep(2)


def add3(value, number):
    while True:
        try:
            number.value += value
            print("number add3 = {0}".format(number.value))
        except Exception as e:
            raise e
        time.sleep(2)


def add4(d):
    while True:
        d[4] = str(random.randint(10, 20))
        time.sleep(1)


def add5(d):
    while True:
        d[5] = str(random.randint(20, 30))
        time.sleep(1)


def test(room_id, alg_result):
    while True:
        alg_result[room_id] = str(random.randint(20, 30))
        time.sleep(0.1)


if __name__ == '__main__':
    print("start main")
    number = Value('d', 0)  # 共用的内存地址
    manager = Manager()  # 字典方式
    dict1 = manager.dict()
    p1 = multiprocessing.Process(target=test, args=(1, dict1))
    p2 = multiprocessing.Process(target=test, args=(2, dict1))
    p1.start()
    p2.start()
    print("end main")
    while True:
        print("dict1", dict1)
        time.sleep(1)

猜你喜欢

转载自www.cnblogs.com/hd13/p/10894450.html