Lock组件
当我们用多进程来读写文件的时候,如果一个进程是写文件,一个进程是读文件,如果两个文件同时进行,肯定是不行的,必须是文件写结束以后,才可以进行读操作。或者是多个进程在共享一些资源的时候,同时只能有一个进程进行访问,那就要有一个锁机制进行控制。
需求:
一个进程写入一个文件,一个进程追加文件,一个进程读文件,同时启动起来
我们可以通过进程的join()方法来实现,但是为了学习Lock,用Lock来实现。
先看不加锁程序,在看加锁程序,最后比较两个程序的区别
不加锁:
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-12-11 15:15
-
# @file :4.py
-
-
-
import multiprocessing
-
import time
-
-
def add1(lock, value, number):
-
print("start add{0} number = {1}".format(value, number))
-
for i in xrange(1, 5):
-
number += value
-
time.sleep(1)
-
print("add{0} number = {1}".format(value, number))
-
-
def add3(lock, value, number):
-
print("start add{0} number = {1}".format(value, number))
-
for i in xrange(1, 5):
-
number += value
-
time.sleep(1)
-
print("add{0} number = {1}".format(value, number))
-
-
-
-
if __name__ == "__main__":
-
lock = multiprocessing.Lock()
-
number = 0
-
pw = multiprocessing.Process(target=add1, args=(lock, 1, number))
-
pa = multiprocessing.Process(target=add3, args=(lock, 3, number))
-
pw.start()
-
pa.start()
-
print("main process end.")
执行结果:
start add1 number = 0
start 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
加锁:
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-12-11 14:53
-
# @file :3.py
-
-
-
import multiprocessing
-
import time
-
-
def add1(lock, value, number):
-
with lock:
-
print("start add{0} number = {1}".format(value, number))
-
for i in xrange(1, 5):
-
number += value
-
time.sleep(1)
-
print("add{0} number = {1}".format(value, number))
-
-
-
def add3(lock, value, number):
-
lock.acquire()
-
print("start add{0} number = {1}".format(value, number))
-
try:
-
for i in xrange(1, 5):
-
number += value
-
time.sleep(1)
-
print("add{0} number = {1}".format(value, number))
-
finally:
-
lock.release()
-
-
-
if __name__ == "__main__":
-
lock = multiprocessing.Lock()
-
number = 0
-
pw = multiprocessing.Process(target=add1, args=(lock, 1, number))
-
pa = multiprocessing.Process(target=add3, args=(lock, 3, number))
-
pw.start()
-
pa.start()
-
print("main process end.")
执行结果:
start add1 number = 0
add1 number = 1
add1 number = 2
add1 number = 3
add1 number = 4
start add3 number = 0
add3 number = 3
add3 number = 6
add3 number = 9
add3 number = 12
共享内存
python的multiprocessing模块也给我们提供了共享内存的操作
一般的变量在进程之间是没法进行通讯的,multiprocessing给我们提供了Value和Array模块,他们可以在不同的进程中共同使用
代码实例
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-12-11 15:24
-
# @file :5.py
-
-
from multiprocessing import Process, Value, Array
-
def f(n, a, m):
-
n.value = 3.1415927
-
m = 20
-
for i in range(len(a)):
-
a[i] = -a[i]
-
print(m)
-
-
if __name__ == '__main__':
-
num = Value('d', 0.0)
-
arr = Array('i', range(10))
-
m = 10
-
p = Process(target=f, args=(num, arr, m))
-
p.start()
-
p.join()
-
print(num.value)
-
print(arr[:])
-
print(m)
执行结果:
20
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
10
阅读(1487) | 评论(0) | 转发(0) |