如何在 python 的多处理中创建嵌套共享对象?
Posted
技术标签:
【中文标题】如何在 python 的多处理中创建嵌套共享对象?【英文标题】:how do you create nested shared objects in multi processing in python? 【发布时间】:2021-10-06 18:45:04 【问题描述】:如何在多处理中创建嵌套的可共享对象? - 比如嵌套列表?
import multiprocessing
if __name__ == '__main__':
manager = multiprocessing.Manager()
processors = 5
'''This is what I am trying to replicate'''
# MASTER = [[[], [], []] for _ in range(processors)]
MASTER = manager.list()
for number in range(processors):
MASTER.append(manager.list())
index = MASTER.index(number)
for _ in range(3):
MASTER[index].append(manager.list())
print(MASTER)
我在尝试运行此代码时收到 ValueError。
Traceback (most recent call last):
File "C:/Users/engli/PycharmProjects/pythonProject/16/04/15/07/newtest.py", line 12, in <module>
index = MASTER.index(number)
File "<string>", line 2, in index
【问题讨论】:
【参考方案1】:找到了。
import multiprocessing
if __name__ == '__main__':
manager = multiprocessing.Manager()
processors = 5
MASTER = manager.list()
for number in range(processors):
MASTER.append(manager.list())
for x in MASTER:
x.append(manager.list())
x.append(manager.list())
x.append(manager.list())
【讨论】:
以上是关于如何在 python 的多处理中创建嵌套共享对象?的主要内容,如果未能解决你的问题,请参考以下文章