python多进程共享内存
Posted betterthanever_victor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python多进程共享内存相关的知识,希望对你有一定的参考价值。
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多进程共享内存的主要内容,如果未能解决你的问题,请参考以下文章
python学习笔记——多进程中共享内存Value & Array