I am running a multi-process code in Python with a shared array. 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.
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 Answer 1
To print elements inside the Array do the following:
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[:]))
answered May 31, 2020 at 16:34
Artiom Kozyrev
3,9062 gold badges18 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-py
<SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_int_Array_2 object at 0x7f7bbc63df28>>. I am running Ubuntu.