1

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]
4
  • Which python version are you using ? Also, i can't reproduce this behaviour and your code will not print [1, 2] but i'll print anothing thing. Commented May 31, 2020 at 0:01
  • Can you turn this into a short working example? Commented May 31, 2020 at 16:36
  • After fixing the syntax errors your code prints <SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_int_Array_2 object at 0x7f7bbc63df28>>. I am running Ubuntu. Commented May 31, 2020 at 16:40
  • Can you include your operating system and how you run this code (eg, command line, jupyter, etc...)? Commented May 31, 2020 at 16:42

1 Answer 1

3

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[:]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.