首页 > 系统服务 > 详细

多进程共享数据,真正的通信Manager

时间:2018-05-18 15:13:36      阅读:196      评论:0      收藏:0      [点我收藏+]
Managers

A manager object returned by Manager() controls a server process which holds Python objects and allows other processes to manipulate them using proxies.

A manager returned by Manager() will support types list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Barrier, Queue, Value and Array. For example,

 

from multiprocessing import Process,Manager
import threading,time,os

def sub(d,l):
    d[os.getpid()] = os.getpid()
    l.append(os.getppid())
    print(l)
    #print(d)
if __name__ == ‘__main__‘:
    with Manager() as M: #with..as的作用是把Manager()生成一个别名M,并只有在with内有效
        d = M.dict()    #用了with,下面也可以不用。
        l = Manager().list(range(5))
        p_list = []    #这个列表保存生成的多个进程 ,方便促一join()。等待全部完成,
        for i in range(10):
            t = Process(target=sub,args=(d,l))
            t.start()
            p_list.append(t)
        for i in p_list:
            i.join()
        print(d,l)

  ...

 

多进程共享数据,真正的通信Manager

原文:https://www.cnblogs.com/alex-hrg/p/9056008.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!