import multiprocessing import time defadd(number,value,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")
结果为:
1 2 3 4 5 6 7 8 9 10 11 12 13
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
import multiprocessing import time defadd(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()
main end init add1 number = 0#add1优先抢到锁,优先执行 add1 number = 1 add1 number = 2 add1 number = 3 add1 number = 4 add1 number = 5 init add3 number = 0#add3被阻塞,等待add1执行完成,释放锁后执行add3 add3 number = 3 add3 number = 6 add3 number = 9 add3 number = 12 add3 number = 15 #注意观察上面add3部分,虽然在add1部分已经将number加到了5,但是由于number变量只是普通变量,不能在各个进程之间进行共享,因此add3开始还要从0开始加
使用Value和Array来进行内存之中的共享通信
一般的变量在进程之间是没法进行通讯的,multiprocessing 给我们提供了 Value 和 Array 模块,他们可以在不通的进程中共同使用。
#add3开始时是在add1的基础上来进行加的 main end init add1 number = 0 ***************add1 has added*********** 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
import multiprocessing import time defadd(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()
defchange(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)) print (arr[:]) 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")
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] init add3 number = 0 ***************add3 has added*********** [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] main end add3 number = 3 ***************add3 has added*********** add3 number = 6 ***************add3 has added*********** add3 number = 9 ***************add3 has added*********** add3 number = 12 ***************add3 has added*********** add3 number = 15 init add1 number = 15 ***************add1 has added*********** add1 number = 16 ***************add1 has added*********** add1 number = 17 ***************add1 has added*********** add1 number = 18 ***************add1 has added*********** add1 number = 19 ***************add1 has added*********** add1 number = 20