Python multiprocessing Value和Manager实例

原因:可以通过 multiprocessing.Value 和 multiprocessing.Array 或者 multiprocessing.sharedctpyes 来实现内存共享,也可以通过服务器进程管理器 Manager() 来实现数据和状态的共享。这两种方式各有优势,总体来说共享内存的方式更快,效率更高,但服务器进程管理器 Manager() 使用起来更为方便,并且支持本地和远程内存共享。

Value实例:

# coding=utf-8
import time
import logging
import multiprocessing

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [*] %(processName)s %(message)s"
)


# 同步访问(子进程由操作系统分配,子进程不是)
def func (val):
    for i in range(10):
        time.sleep(0.1)
        with val.get_lock():  # 仍然需要使用 get_lock 方法来获取锁对象
            val.value += 1
            # logging.info(val.value)


def main (ctx):
    v = ctx.Value("i", 0)  # 使用 value 来共享内存
    processList = [ctx.Process(target=func, args=(v,)) for _ in range(9)]
    [task.start() for task in processList]
    [task.join() for task in processList]
    logging.info(v.value)


if __name__ == '__main__':
    # windows 启动方式
    multiprocessing.set_start_method('spawn')
    # 获取上下文
    ctx = multiprocessing.get_context('spawn')
    main(ctx)

输出:

2019-10-04 15:31:16,410 [*] SpawnProcess-4 1
2019-10-04 15:31:16,432 [*] SpawnProcess-3 2
2019-10-04 15:31:16,459 [*] SpawnProcess-7 3
2019-10-04 15:31:16,476 [*] SpawnProcess-2 4
2019-10-04 15:31:16,480 [*] SpawnProcess-5 5
2019-10-04 15:31:16,522 [*] SpawnProcess-8 6
2019-10-04 15:31:16,522 [*] SpawnProcess-4 7
2019-10-04 15:31:16,522 [*] SpawnProcess-6 8
2019-10-04 15:31:16,545 [*] SpawnProcess-3 9
2019-10-04 15:31:16,545 [*] SpawnProcess-1 10
2019-10-04 15:31:16,545 [*] SpawnProcess-9 11
2019-10-04 15:31:16,567 [*] SpawnProcess-7 12
2019-10-04 15:31:16,590 [*] SpawnProcess-5 13
2019-10-04 15:31:16,590 [*] SpawnProcess-2 14
2019-10-04 15:31:16,627 [*] SpawnProcess-6 15
2019-10-04 15:31:16,627 [*] SpawnProcess-4 16
2019-10-04 15:31:16,627 [*] SpawnProcess-8 17
2019-10-04 15:31:16,645 [*] SpawnProcess-3 18
2019-10-04 15:31:16,645 [*] SpawnProcess-1 19
2019-10-04 15:31:16,662 [*] SpawnProcess-9 20
2019-10-04 15:31:16,684 [*] SpawnProcess-7 21
2019-10-04 15:31:16,691 [*] SpawnProcess-2 22
2019-10-04 15:31:16,691 [*] SpawnProcess-5 23
2019-10-04 15:31:16,728 [*] SpawnProcess-4 24
2019-10-04 15:31:16,728 [*] SpawnProcess-6 25
2019-10-04 15:31:16,728 [*] SpawnProcess-8 26
2019-10-04 15:31:16,746 [*] SpawnProcess-3 27
2019-10-04 15:31:16,746 [*] SpawnProcess-1 28
2019-10-04 15:31:16,767 [*] SpawnProcess-9 29
2019-10-04 15:31:16,790 [*] SpawnProcess-7 30
2019-10-04 15:31:16,791 [*] SpawnProcess-5 31
2019-10-04 15:31:16,792 [*] SpawnProcess-2 32
2019-10-04 15:31:16,837 [*] SpawnProcess-6 33
2019-10-04 15:31:16,837 [*] SpawnProcess-4 34
2019-10-04 15:31:16,838 [*] SpawnProcess-8 35
2019-10-04 15:31:16,861 [*] SpawnProcess-3 36
2019-10-04 15:31:16,861 [*] SpawnProcess-1 37
2019-10-04 15:31:16,868 [*] SpawnProcess-9 38
2019-10-04 15:31:16,891 [*] SpawnProcess-7 39
2019-10-04 15:31:16,907 [*] SpawnProcess-2 40
2019-10-04 15:31:16,907 [*] SpawnProcess-5 41
2019-10-04 15:31:16,937 [*] SpawnProcess-6 42
2019-10-04 15:31:16,938 [*] SpawnProcess-4 43
2019-10-04 15:31:16,954 [*] SpawnProcess-8 44
2019-10-04 15:31:16,977 [*] SpawnProcess-9 45
2019-10-04 15:31:16,977 [*] SpawnProcess-3 46
2019-10-04 15:31:16,978 [*] SpawnProcess-1 47
2019-10-04 15:31:17,000 [*] SpawnProcess-7 48
2019-10-04 15:31:17,007 [*] SpawnProcess-5 49
2019-10-04 15:31:17,007 [*] SpawnProcess-2 50
2019-10-04 15:31:17,050 [*] SpawnProcess-6 51
2019-10-04 15:31:17,051 [*] SpawnProcess-4 52
2019-10-04 15:31:17,054 [*] SpawnProcess-8 53
2019-10-04 15:31:17,078 [*] SpawnProcess-3 54
2019-10-04 15:31:17,078 [*] SpawnProcess-9 55
2019-10-04 15:31:17,079 [*] SpawnProcess-1 56
2019-10-04 15:31:17,101 [*] SpawnProcess-7 57
2019-10-04 15:31:17,120 [*] SpawnProcess-5 58
2019-10-04 15:31:17,120 [*] SpawnProcess-2 59
2019-10-04 15:31:17,167 [*] SpawnProcess-4 60
2019-10-04 15:31:17,167 [*] SpawnProcess-8 61
2019-10-04 15:31:17,168 [*] SpawnProcess-6 62
2019-10-04 15:31:17,191 [*] SpawnProcess-9 63
2019-10-04 15:31:17,191 [*] SpawnProcess-1 64
2019-10-04 15:31:17,192 [*] SpawnProcess-3 65
2019-10-04 15:31:17,213 [*] SpawnProcess-7 66
2019-10-04 15:31:17,237 [*] SpawnProcess-2 67
2019-10-04 15:31:17,237 [*] SpawnProcess-5 68
2019-10-04 15:31:17,268 [*] SpawnProcess-4 69
2019-10-04 15:31:17,285 [*] SpawnProcess-6 70
2019-10-04 15:31:17,285 [*] SpawnProcess-8 71
2019-10-04 15:31:17,293 [*] SpawnProcess-9 72
2019-10-04 15:31:17,293 [*] SpawnProcess-3 73
2019-10-04 15:31:17,293 [*] SpawnProcess-1 74
2019-10-04 15:31:17,314 [*] SpawnProcess-7 75
2019-10-04 15:31:17,337 [*] SpawnProcess-2 76
2019-10-04 15:31:17,338 [*] SpawnProcess-5 77
2019-10-04 15:31:17,378 [*] SpawnProcess-4 78
2019-10-04 15:31:17,400 [*] SpawnProcess-3 79
2019-10-04 15:31:17,400 [*] SpawnProcess-1 80
2019-10-04 15:31:17,400 [*] SpawnProcess-9 81
2019-10-04 15:31:17,401 [*] SpawnProcess-8 82
2019-10-04 15:31:17,402 [*] SpawnProcess-6 83
2019-10-04 15:31:17,422 [*] SpawnProcess-7 84
2019-10-04 15:31:17,444 [*] SpawnProcess-5 85
2019-10-04 15:31:17,444 [*] SpawnProcess-2 86
2019-10-04 15:31:17,512 [*] SpawnProcess-9 87
2019-10-04 15:31:17,512 [*] SpawnProcess-6 88
2019-10-04 15:31:17,513 [*] SpawnProcess-8 89
2019-10-04 15:31:17,513 [*] SpawnProcess-1 90
2019-10-04 15:31:17,549 [*] MainProcess 90

Manager实例:

# coding=utf-8
import time
import logging
import multiprocessing

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [*] %(processName)s %(message)s"
)


def func (ns, x, y):
    logging.info(f"func 处理前: {ns}")
    x.append(1)
    y.append("a")
    ns.x = x  # 将可变对象也作为参数传入
    ns.y = y
    logging.info(f"func 处理后: {ns}")


def main (ctx):
    manager = ctx.Manager()
    ns = manager.Namespace()  # 命名参数
    ns.x = []  # manager 内部包括可变对象
    ns.y = []
    
    logging.info(f"main 处理前: {ns}")
    p = ctx.Process(target=func, args=(ns, ns.x, ns.y))
    p.start()
    p.join()
    logging.info(f"main 处理后:{ns}")


if __name__ == '__main__':
    # windows 启动方式
    multiprocessing.set_start_method('spawn')
    # 获取上下文
    ctx = multiprocessing.get_context('spawn')
    main(ctx)

输出:

2019-10-04 16:14:41,190 [*] MainProcess main 处理前: Namespace(x=[], y=[])
2019-10-04 16:14:41,268 [*] SpawnProcess-2 func 处理前: Namespace(x=[], y=[])
2019-10-04 16:14:41,268 [*] SpawnProcess-2 func 处理后: Namespace(x=[1], y=['a'])
2019-10-04 16:14:41,284 [*] MainProcess main 处理后:Namespace(x=[1], y=['a'])
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值