8-1多进程锁和共享内存

Posted zhan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8-1多进程锁和共享内存相关的知识,希望对你有一定的参考价值。

多进程Lock组件
当我们使用多进程读写文件时,一个进程写文件,一个进程读文件。如果两个进程同时进行,肯定不行,必须等写结束后,才可以进行多操作。或者多个进程在共享一些资源时,同时只能有一个进程进行访问,需要一个锁机制控制。

import multiprocessing
import time
lock = multiprocessing.Lock()
def add(number,value,lock):
with lock:
print "init add{0} number={1}".format(value,number)
for i in xrange(1,5):
number += value
time.sleep(1)
print "add{0} num = {1}".format(value,number)


if __name__ == "__main__":
number = 0
lock = multiprocessing.Lock()
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 num = 1
add3 num = 3
add1 num = 2
add3 num = 6
add1 num = 3
add3 num = 9
add1 num = 4
add3 num = 12

加锁操作with lock 方式使用:
#!/usr/bin/env python
#coding:utf8
# import multiprocessing
# lock = multiprocessing.Lock()
# lock.acquire() 获得锁
# lock.release() 释放锁
#with lock 方式使用
# 不加锁
# number + 1
# number + 3

import multiprocessing
import time
lock = multiprocessing.Lock()
def add(number,value,lock):
with lock:
print "init add{0} number={1}".format(value,number)
for i in xrange(1,5):
number += value
time.sleep(1)
print "add{0} num = {1}".format(value,number)


if __name__ == "__main__":
number = 0
lock = multiprocessing.Lock()
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 num = 1
add1 num = 2
add1 num = 3
add1 num = 4
init add3 number=0
add3 num = 3
add3 num = 6
add3 num = 9
add3 num = 12

lock.acquire方式使用

import multiprocessing
import time
lock = multiprocessing.Lock()
def add(number,value,lock):
# with lock:
lock.acquire()
try:
print "init add{0} number={1}".format(value,number)
for i in xrange(1,5):
number += value
time.sleep(1)
print "add{0} num = {1}".format(value,number)
except Exception as e:
raise e
finally:
lock.release()

if __name__ == "__main__":
number = 0
lock = multiprocessing.Lock()
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 num = 1
add1 num = 2
add1 num = 3
add1 num = 4
init add3 number=0
add3 num = 3
add3 num = 6
add3 num = 9
add3 num = 12

共享内存:
python的multiprocessing模块也给我们提供了共享内存的操作
一般的变量在进程之间是没法进行通讯的,multiprocessing给我们提供了
import multiprocessing
import time
def change(arr):
for i in range(len(arr)):
arr[i] = -arr[i]
if __name__ == "__main__":
print "main start"
arr = multiprocessing.Array(‘i‘,range(10))
print arr[:]
p3 = multiprocessing.Process(target=change,args=(arr,))
p3.start()
p3.join()
print arr[:]


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, 5):
old_number_value = number.value
number.value += add_value
print " add{0} num {2} = {0} + {1}".format(add_value,old_number_value, number.value)
print "#####add{0} has added #####".format(add_value)
time.sleep(1)
print "add{0} num = {1}".format(add_value, number.value)
except Exception as e:
raise e

if __name__ == "__main__":
print "main start"
number = multiprocessing.Value(‘i‘,0)
print number
p1 = multiprocessing.Process(target=add,args=(number,1))
p2 = multiprocessing.Process(target=add,args=(number,3))
p1.start()
p2.start()
print "main end"
运行结果:
main start
<Synchronized wrapper for c_long(0)>
main end
init add1 number=0
add1 num 1 = 1 + 0
#####add1 has added #####
init add3 number=1
add3 num 4 = 3 + 1
#####add3 has added #####
add1 num = 4
add1 num 5 = 1 + 4
#####add1 has added #####
add3 num = 5
add3 num 8 = 3 + 5
#####add3 has added #####
add1 num = 8
add1 num 9 = 1 + 8
#####add1 has added #####
add3 num = 9
add3 num 12 = 3 + 9
#####add3 has added #####
add1 num = 12
add1 num 13 = 1 + 12
#####add1 has added #####
add3 num = 13
add3 num 16 = 3 + 13
#####add3 has added #####
add1 num = 16
add3 num = 16

Process finished with exit code 0

 

以上是关于8-1多进程锁和共享内存的主要内容,如果未能解决你的问题,请参考以下文章

共享内存原理

共享内存

linux进程间通信--共享内存

UNIX网络编程:互斥锁和条件变量

信号量

033 Android多进程-共享内存