How can I do operation on the array from the buffer?
import multiprocessing as mp
import numpy as np
# Initialization of the array
n_elem = 10
buffer = mp.RawArray('i', n_elem)
from_buffer = np.reshape(np.frombuffer(buffer, dtype=np.uint32), -1)
for _ in range(n_elem):
from_buffer[_] = _
print(from_buffer)
>> [0 1 2 3 4 5 6 7 8 9]
Method that works to roll the array:
for _ in range(n_elem-1):
from_buffer[_] = from_buffer[_ + 1]
from_buffer[-1] = 10
from_buffer2 = np.reshape(np.frombuffer(buffer, dtype=np.uint32), -1)
print(from_buffer2)
>> [ 1 2 3 4 5 6 7 8 9 10]
When from_buffer2
is "reinitialized", it stops "updating" buffer array:
from_buffer2 = np.reshape(np.frombuffer(buffer, dtype=np.uint32), -1)
from_buffer2 = np.roll(from_buffer2, -1, axis=0)
from_buffer2[-1] = 0
from_buffer = np.reshape(np.frombuffer(buffer, dtype=np.uint32), -1)
print(from_buffer)
>> [ 1 2 3 4 5 6 7 8 9 10]
I do not understand completely what is going on. When the array is generated from buffer, a change on from_buffer
is visible on from_buffer2
as well when using index of the array.
Is there a way to manipulate the array differently?