1

I am trying to use the new shared memory example in python 3.8 from this link https://docs.python.org/3/library/multiprocessing.shared_memory.html

# In the first Python interactive shell import numpy as np a = np.array([1, 1, 2, 3, 5, 8]) # Start with an existing NumPy array from multiprocessing import shared_memory shm = shared_memory.SharedMemory(create=True, size=a.nbytes) # Now create a NumPy array backed by shared memory b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf) b[:] = a[:] # Copy the original data into shared memory shname = shm.name # We did not specify a name so one was chosen for us print(shname) print(a) print(b) # In either the same shell or a new Python shell on the same machine import numpy as np from multiprocessing import shared_memory # Attach to the existing shared memory block existing_shm = shared_memory.SharedMemory(name=shname) # Note that a.shape is (6,) and a.dtype is np.int64 in this example c = np.ndarray((6,), dtype=np.int64, buffer=existing_shm.buf) print(c) c[-1] = 888 print(c) # Back in the first Python interactive shell, b reflects this change # Clean up from within the second Python shell del c # Unnecessary; merely emphasizing the array is no longer used existing_shm.close() # Clean up from within the first Python shell del b # Unnecessary; merely emphasizing the array is no longer used shm.close() shm.unlink() # Free and release the shared memory block at the very end 

In the example, the output of c should be array([1, 1, 2, 3, 5, 8]) however when I run this I get:

wnsm_26020d1b [1 1 2 3 5 8] [1 1 2 3 5 8] [ 4294967297 12884901890 34359738373 0 0 0] [ 4294967297 12884901890 34359738373 0 0 888] 

Did I totally miss something? Anyone else have this result?

2
  • What OS are you on? Commented Nov 20, 2019 at 3:21
  • @user2357112supportsMonica windows, you solved it thanks! Commented Nov 20, 2019 at 12:25

1 Answer 1

6

Your c array needs to be created with the same dtype as b, but it's not. From the output shown, we can tell you're on Windows, where NumPy's default integer size is 32 bits, not 64. You've specified a dtype of np.int64 for the second array, but b is using the default size of 32 bits.

Use np.int_ for the second array to use NumPy's default integer dtype, or specify an explicit dtype for both a and c (which will also be used by b, because b's dtype comes from a).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.