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)
原文地址:https://www.cnblogs.com/BetterThanEver_Victor/p/10956928.html
时间: 2024-11-13 09:39:58