共享数组多处理 Python
Shared array multiprocessing Python
我是运行一个共享数组的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]
要打印 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[:]))
我是运行一个共享数组的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]
要打印 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[:]))