码迷,mamicode.com
首页 > 编程语言 > 详细

python多进程共享内存

时间:2019-05-31 19:20:28      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:art   tip   python   ESS   name   start   style   color   pen   

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


def run_proc(dict,slip,lock):
    tmp=dict[slip].copy()
    for k in dict[slip]:
        tmp[k]=k+tmp[k]
    print(Run child process{ch}... .format(ch=os.getpid()))
    with lock:
        dict[slip]=tmp



if __name__ == __main__:
    print(Parent process %s. % os.getpid())
    dict=[{1:a,2:b,3:c},{4:d,5:e,6:f},{7:g,8:h,9:i},{10:j,11:k,12:l}]
    m = Manager()
    share_ls = m.list()
    for d in dict:
        share_ls.append(d)
    print(share_ls)
    processes = list()
    lock = Lock()#多进程锁
    for i in range(len(dict)):
        p = Process(target=run_proc, args=(share_ls,i,lock))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()
    print(Process end.)
    print(share_ls)

 

python多进程共享内存

标签:art   tip   python   ESS   name   start   style   color   pen   

原文地址:https://www.cnblogs.com/BetterThanEver_Victor/p/10956928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!