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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM