
You can take a memory view of the array directly: memoryview(array.array("Q", range(1000))) If your exact use-case is writing to a SharedMemory, then I don't think there is any simple way to do it without creating some intermediate memory buffer. (other than using struct.pack_into, or packing the values completely manually) On 10/10/2021 16:18, Facundo Batista wrote:
El dom, 10 de oct. de 2021 a la(s) 11:50, Serhiy Storchaka (storchaka@gmail.com) escribió:
10.10.21 17:19, Facundo Batista пише:
I have a long list of nums (several millions), ended up doing the following:
struct.pack_into(f'{len(nums)}Q', buf, 0, *nums) Why not use array('Q', nums)? You mean `array` from the `array` module? The only way I see using it is like the following:
shm = shared_memory.SharedMemory(create=True, size=total_size) a = array.array('Q', nums) shm.buf[l_offset:r_offset] = a.tobytes() But I don't like it because of the `tobytes` call, which will produce a huge bytearray only to insert it in the shared memory buffer.
That's why I liked `pack_into`, because it will write directly into the memory view.
Or I'm missing something?
Thanks!