简体   繁体   English

共享数组多处理 Python

[英]Shared array multiprocessing Python

I am running a multi-process code in Python with a shared array.我正在使用共享数组在 Python 中运行多进程代码。 The problem is that I can't initialise that array...问题是我无法初始化该数组...
To share an array in a multi-process program I have read I need to use multiprocessing.Array, but when I try it as in the code below it is not printing anything + I don't have error messages.要在我读过的多进程程序中共享一个数组,我需要使用 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]

To print elements inside the Array do the following:要打印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[:]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM