python学习—Day41—多进程锁与多进程共享内存

多进程锁:

需求:

一个进程写入一个文件,一个进程追加文件,一个进程读文件,同时启动起来

我们可以通过join()方法实现,这里为了学习Lock,用lock实现。


先看不加锁程序,再看加锁程序,对比两者的区别。


#@File :jinchenglock.py
import multiprocessing

import time

lock = multiprocessing.Lock()
lock.acquire()                        #获得锁
lock.release()                        #释放锁.这两者可以直接使用with lock一句实现
#不加锁
# number  +1
# number  +3
def add(number, value, lock):
    # with lock:
        print("init add{0} number = {1}".format(value, number))
        for i in xrange(1, 6):
            number += value
            time.sleep(1)
            print("add{0} number = {1}".format(value, number))
if __name__ == "__main__":
    lock = multiprocessing.Lock()
    number = 0
    p1 = multiprocessing.Process(target=add, args=(number, 1, lock))
    p2 = multiprocessing.Process(target=add, args=(number, 3, lock))
    p1.start()
    p2.start()
    print ("main end")
main end
init add1 number = 0
init add3 number = 0
add1 number = 1
add3 number = 3
add1 number = 2
add3 number = 6
add1 number = 3
add3 number = 9
add1 number = 4
add3 number = 12
add1 number = 5
add3 number = 15


使用锁:

#@File :jinchenglock.py
import multiprocessing

import time

lock = multiprocessing.Lock()
lock.acquire()                        #获得锁
lock.release()                        #释放锁.这两者可以直接使用with lock一句实现
#不加锁
# number  +1
# number  +3
def add(number, value, lock):
    with lock:
        print("init add{0} number = {1}".format(value, number))
        for i in xrange(1, 6):
            number += value
            time.sleep(1)
            print("add{0} number = {1}".format(value, number))
if __name__ == "__main__":
    lock = multiprocessing.Lock()
    number = 0
    p1 = multiprocessing.Process(target=add, args=(number, 1, lock))
    p2 = multiprocessing.Process(target=add, args=(number, 3, lock))
    p1.start()
    p2.start()
    print ("main end")
main end
init add1 number = 0
add1 number = 1
add1 number = 2
add1 number = 3
add1 number = 4
add1 number = 5
init add3 number = 0
add3 number = 3
add3 number = 6
add3 number = 9
add3 number = 12
add3 number = 15


#@File :jinchenglock.py
import multiprocessing
import time

lock = multiprocessing.Lock()
lock.acquire()                        #获得锁
lock.release()                        #释放锁.这两者可以直接使用with lock一句实现
#不加锁
# number  +1
# number  +3
def add(number, value, lock):
    lock.acquire()
    try:
        print("init add{0} number = {1}".format(value, number))
        for i in xrange(1, 6):
            number += value
            time.sleep(1)
            print("add{0} number = {1}".format(value, number))
    except Exception as e:
        raise e
    finally:
        lock.release()
if __name__ == "__main__":
    lock = multiprocessing.Lock()
    number = 0
    p1 = multiprocessing.Process(target=add, args=(number, 1, lock))
    p2 = multiprocessing.Process(target=add, args=(number, 3, lock))
    p1.start()
    p2.start()
    print ("main end")
main end
init add1 number = 0
add1 number = 1
add1 number = 2
add1 number = 3
add1 number = 4
add1 number = 5
init add3 number = 0
add3 number = 3
add3 number = 6
add3 number = 9
add3 number = 12
add3 number = 15


多进程共享内存:

#@File :jinchenglock.py
import multiprocessing
import time

def add(number, add_value):
    try:
        print("init add{0} number = {1}".format(add_value, number.value))
        for i in xrange(1, 6):
            number.value += add_value
            print("###############  add{0} has added ########### ".format(add_value))
            time.sleep(1)
            print("add{0} number = {1}".format(add_value, number.value))
    except Exception as e:
        raise e
if __name__ == "__main__":
    number = multiprocessing.Value('i', 0)
    p1 = multiprocessing.Process(target=add, args=(number, 1))
    p2 = multiprocessing.Process(target=add, args=(number, 3))
    p1.start()
    p2.start()
    print ("main end")
main end
init add1 number = 0
###############  add1 has added ########### 
init add3 number = 1
###############  add3 has added ########### 
add1 number = 4
###############  add1 has added ########### 
add3 number = 5
###############  add3 has added ########### 
add1 number = 8
###############  add1 has added ########### 
add3 number = 9
###############  add3 has added ########### 
add1 number = 12
###############  add1 has added ########### 
add3 number = 13
###############  add3 has added ########### 
add1 number = 16
add3 number = 16
###############  add1 has added ########### 
###############  add3 has added ########### 
add1 number = 20
add3 number = 20


#@File :jinchenglock.py
import multiprocessing
import time
# Value()   数值
# Array()   数组

def add(number, add_value, lock):
    lock.acquire()
    try:
        print("init add{0} number = {1}".format(add_value, number.value))
        for i in xrange(1, 6):
            number.value += add_value
            print("###############  add{0} has added ########### ".format(add_value))
            time.sleep(1)
            print("add{0} number = {1}".format(add_value, number.value))
    except Exception as e:
        raise e
    finally:
        lock.release()

def change(arr):
    for i in range(len(arr)):
        arr[i] = -arr[i]

if __name__ == "__main__":
    lock = multiprocessing.Lock()
    number = multiprocessing.Value('i', 0)
    arr = multiprocessing.Array('i', range(10))
    p1 = multiprocessing.Process(target=add, args=(number, 1, lock))
    p2 = multiprocessing.Process(target=add, args=(number, 3, lock))
    p3 = multiprocessing.Process(target=change, args=(arr, ))
    p1.start()
    p2.start()
    p3.start()
    p3.join()
    print(arr[:])
    print ("main end")
init add1 number = 0
###############  add1 has added ########### 
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
main end
add1 number = 1
###############  add1 has added ########### 
add1 number = 2
###############  add1 has added ########### 
add1 number = 3
###############  add1 has added ########### 
add1 number = 4
###############  add1 has added ########### 
add1 number = 5
init add3 number = 5
###############  add3 has added ########### 
add3 number = 8
###############  add3 has added ########### 
add3 number = 11
###############  add3 has added ########### 
add3 number = 14
###############  add3 has added ########### 
add3 number = 17
###############  add3 has added ########### 
add3 number = 20


python学习—Day41—多进程锁与多进程共享内存_第1张图片



你可能感兴趣的:(python学习)