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 > (storch...@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! > _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ONWPTKL3P6VOZEHUDCL7B6CZ5TI3DER6/ Code of Conduct: http://python.org/psf/codeofconduct/