1 from multiprocessing import Process,Manager
 2 import os
 3 def f(d,n):
 4     d[os.getpid()] = os.getppid()#对字典d添加键值对,子进程:父进程
 5     n.append(os.getpid())#将子进程添加到列表里。
 6 
 7 if __name__ == '__main__':
 8     with Manager() as manager:#类似with open获得句柄
 9         l = manager.dict()#用manager创建一个字典
10         n = manager.list(range(5))#用manager创建一个列表并自带5个数据。
11         p_list = []#创建空列表
12         for i in range(5):#创建5个进程进行for循环
13             p = Process(target=f,args=(l,n))
14             p.start()#启动进程
15             p_list.append(p)#将进程添加到列表p_list
16         for res in p_list:#循环列表中进程
17             res.join()#对进程进行关闭
18         print(l,n)#打印5个不同进程对字典和列表的修改结果。

 

posted on 2017-11-29 08:37  专注于区块链开发  阅读(247)  评论(0编辑  收藏  举报