• (八)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

  • 相关阅读:
    数字货币资金费策略
    如何利用CCXT交易数字货币合约
    三分钟玩转微软AI量化投资开源库QLib
    商品期货月度效应的统计
    数字货币无风险收益率又双叒叕扩大了!
    Omega System Trading and Development Club内部分享策略Easylanguage源码 (第二期)
    【mac】安装配置 homebrew, Nginx
    【Typescript+Vue】01. easy
    【python】sudo python -m SimpleHTTPServer
    【windows】docker to vmware
  • 原文地址:https://www.cnblogs.com/pythonlx/p/8025625.html
Copyright © 2020-2023  润新知