需求:
一个进程写入一个文件,一个进程追加文件,一个进程读文件,同时启动起来
我们可以通过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使用锁:
#@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#@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#@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
#@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