__author__ = "Alex Li"

from multiprocessing import Process, Manager
import os
def f(d, l):
d[os.getpid()] =os.getpid()
l.append(os.getpid())
print(l)

if __name__ == '__main__':
with Manager() as manager:
d = manager.dict() #{} #生成一个字典,可在多个进程间共享和传递

l = manager.list(range(5))#生成一个列表,可在多个进程间共享和传递
p_list = []
for i in range(10):
p = Process(target=f, args=(d, l))
p.start()
p_list.append(p)
for res in p_list: #等待结果
res.join()

print(d)
print(l)


'''
Manager()充当进程之间的翻译者,如果有10个进程,就会有10个一模一样数据,才能进行多个进程间共享和传递
'''

相关文章:

  • 2021-09-16
  • 2021-09-16
  • 2021-07-07
  • 2022-02-02
  • 2020-04-24
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-27
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
相关资源
相似解决方案