The Manager can implement the sharing of lists, dictionaries, variables, locks, semaphores, events, and other data. The manager is locked by default.
From multiprocessing import Process, Managerimport osdef f (d, L): d[1] = ' 1 ' d[' 2 '] = 2 d[0.25] = None d[o S.getpid ()]=os.getpid () l.append (Os.getpid ()) #获取进程号 print (l) if __name__ = = ' __main__ ': With Manager () As manager: #manager =manager () d = manager.dict () #生成一个在多个进程之间可以传递共享的字典, L = manager.list (range (5)) # Generates a list that can be passed and shared across multiple processes, with 5 elements p_list = [] #存放结果 for i in range: p = Process (Target=f, args= (d, L)) C12/>p.start () p_list.append (p) for res in p_list: #等待结果 res.join () print (d) print (L)
Operation Result:
C:\abccdxddd\Oldboy\python-3.5.2-embed-amd64\python.exe C:/abccdxddd/oldboy/py_exercise/day10/manager_tes.py[0, 1, 2, 3, 4, 12836][0, 1, 2, 3, 4, 12836, 11324][0, 1, 2, 3, 4, 12836, 11324, 15000][0, 1, 2, 3, 4, 12836, 11324, 15000, 95 76][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960][0, 1, 2, 3, 4 , 12836, 11324, 15000, 9576, 14476, 13960, 14504][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960, 14504, 4208][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960, 14504, 4208, 12984][0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 14476, 13960, 14504, 4208, 12984, 11724]{0.25:none, 1: ' 1 ', 12,836:12,836, 13,960:13,960, 11,724:11,724, 12,984:12,984, 14,476:14,476 , ' 2 ': 2, 9,576:9,576, 4,208:4,208, 14,504:14,504, 15,000:15,000, 11324:11324}[0, 1, 2, 3, 4, 12836, 11324, 15000, 9576, 1447 6, 13960, 14504, 4208, 12984, 11724]process finished with exit code 0
interprocess communication -3 (Manager)