
10.10.21 19:05, Patrick Reader пише:
This still isn't a completely direct write - you're still creating an array in between which is then copied into shm, whereas struct.pack_into writes directly into the shared memory with no intermediate buffer.
And unfortunately you can't do
shm.buf[l_offset:r_offset].cast('Q')[:] = nums
where nums is a plain Python iterable; it has to already be an array, so it still requires a copy.
Yes, I tried this too. Maybe we will add a support of arbitrary iterables. There may be problems with this because we don't know the length of the iterable and can't guarantee that all items are convertible without converting them and saving results in a temporary array. If you do not want to spend additional memory, iterate nums and set items one by one. Or iterate and set them by chunks target = shm.buf[l_offset:r_offset].cast('Q') for i in range(0, len(nums), chunksize): j = min(i + chunksize, len(nums)) target[i:j] = array.array('Q', nums[i:j])