我正在使用共享数组在 Python 中运行多进程代码。问题是我无法初始化那个数组...要在我读过的多进程程序中共享一个数组我需要使用 multiprocessing.Array,但是当我在下面的代码中尝试它时它没有打印任何东西+ 我没有错误信息。import multiprocessing......if __name__ == "__main__": an_array= multiprocessing.Array("i", [1,2]) print(an_array) # why does it not print anything? I was expecting to print [1,2] p1 = multiprocessing.Process(target=function1, args = [an_array, 3]
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
要在 中打印元素,Array请执行以下操作:
import multiprocessing
if __name__ == '__main__':
an_array = multiprocessing.Array("i", [1, 2])
# first choice to print element in Array:
for element in an_array:
print(element)
# second choice to print elements in Array:
print(an_array[:])
# third choice to print elements in Array:
print(list(an_array[:]))
添加回答
举报
0/150
提交
取消