多进程锁和多进程共享内存

时间:2021-05-10 14:57:58

多进程锁和多进程共享内存

多进程锁和多进程共享内存

 

 

例子:对同一个数字进行加法运算
 
没有使用锁的程序如下:
[root@web thread_process]# cat multi3.py
#!/usr/bin/env python

import multiprocessing
import time

def add(number, increment, lock):
    print 'init number{0} = {1}'.format(increment, number)
    for i in xrange(5):
        number += increment
        time.sleep(1)
        print 'add number{0} = {1}'.format(increment, number)


if __name__ == '__main__':
    number = 0
    lock = multiprocessing.Lock()
    p1 = multiprocessing.Process(target=add, args=(number, 1, lock))        //没有加锁时,子进程各自独立运行,两个子进程同时都对number进行加法运算
    p2 = multiprocessing.Process(target=add, args=(number, 3, lock))
    p1.start()
    p2.start()
    print 'main end'
[root@web thread_process]# python multi3.py
main end
init number3 = 0
init number1 = 0
add number3 = 3                            //结果是无序排列的
add number1 = 1
add number3 = 6
add number1 = 2
add number3 = 9
add number1 = 3
add number1 = 4
add number3 = 12
add number1 = 5
add number3 = 15

 

 

使用锁的程序如下:

[root@web thread_process]# cat multi3.py   
#!/usr/bin/env python

import multiprocessing
import time

def add(number, increment, lock):
    with lock:
        print 'init number{0} = {1}'.format(increment, number)
        for i in xrange(5):
            number += increment
            time.sleep(1)
            print 'add number{0} = {1}'.format(increment, 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'


[root@web thread_process]# python multi3.py
main end
init number1 = 0             //谁先抢到锁谁先运行,另外一个被阻塞,直到当前锁被释放
add number1 = 1
add number1 = 2
add number1 = 3
add number1 = 4
add number1 = 5
init number3 = 0
add number3 = 3
add number3 = 6
add number3 = 9
add number3 = 12
add number3 = 15

 

 

加锁的另外一种写法:
[root@web thread_process]# cat multi3.py   
#!/usr/bin/env python

import multiprocessing
import time

def add(number, increment, lock):
    lock.acquire()
    try:
        print 'init number{0} = {1}'.format(increment, number)
        for i in xrange(5):
            number += increment
            time.sleep(1)
            print 'add number{0} = {1}'.format(increment, 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'
[root@web thread_process]# python multi3.py
main end
init number3 = 0
add number3 = 3
add number3 = 6
add number3 = 9
add number3 = 12
add number3 = 15
init number1 = 0
add number1 = 1
add number1 = 2
add number1 = 3
add number1 = 4
add number1 = 5

 

多进程锁和多进程共享内存

 

Value的例子:

[root@web thread_process]# cat multi4.py
#!/usr/bin/env python

import multiprocessing
import time

def add(number, increment):
    try:
        print 'init number{0} = {1}'.format(increment, number.value)
        for i in xrange(5):
            number.value += increment
            time.sleep(1)
            print 'add number{0} = {1}'.format(increment, number.value)
    except Exception as e:
        raise e


if __name__ == '__main__':
    number = multiprocessing.Value('i', 0)      //定义共享对象
#    number.value                                //获得共享对象
    p1 = multiprocessing.Process(target=add, args=(number, 1))
    p2 = multiprocessing.Process(target=add, args=(number, 3))
    p1.start()
    p2.start()
    print 'main end'

#加不加锁都可以,加锁的话显示的结果顺序会好看点

[root@web thread_process]# python multi4.py
main end
init number1 = 0             //其中一个子进程对number赋值为0
init number3 = 1             //另一个子进程获得共享的number的值0,然后+1=1
add number3 = 4
add number1 = 7
add number3 = 8
add number1 = 11
add number1 = 12
add number3 = 13
add number3 = 16
add number1 = 19
add number3 = 20             //区别:没有定义number为共享对象时,number3最后的值为15,number1的值为5
add number1 = 20             //      定义number为共享对象后,两个子进程每次做加法运算时获得的number的值都为最新的值,所以最后number=20

 

 

Array的例子:

[root@web thread_process]# cat multi5.py
#!/usr/bin/env python

import multiprocessing

def func(arr):
    for i in xrange(len(arr)):
        arr[i] = -arr[i]


if __name__ == '__main__':
    arr = multiprocessing.Array('i', range(10))
    p = multiprocessing.Process(target=func, args=(arr,))
    print type(arr[:])
    print arr[:]
    p.start()
    p.join()        //必须阻塞主进程,否则主进程退出了,子进程也跟着退出,func函数没有执行,arr[:]没有任何改变
    print arr[:]


[root@web thread_process]# python multi5.py
<type 'list'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]