I have some prolem about SharedMemory in python3.8,any help will be good.
Question 1.
SharedMemory has one parameter SIZE,the doc tell me the unit is byte.I created a instance of 1 byte size,then, let shm.buf=bytearray[1,2,3,4], it can work and no any exception!why?
Question 2.
why print buffer is a memory address?
why i set size is 1byte,but result show it allocate 4096byte?
why buffer address and buffer[3:4] address is 3X16X16byte far away?
why buffer[3:4] address same as buffer[1:3] address?
from multiprocessing import shared_memory, Process def writer(buffer): print(buffer) # output: <memory at 0x7f982b6234c0> buffer[:5] = bytearray([1, 2, 3, 4, 5]) buffer[4] = 12 print(buffer[3:4]) # output: <memory at 0x7f982b6237c0> print(buffer[1:3]) # output: <memory at 0x7f982b6237c0> if __name__ == '__main__': shm = shared_memory.SharedMemory('my_memory', create=True, size=1) print(shm.size) # output: 4096 writer(shm.buf) print('shm is :', shm) # output: shm is : SharedMemory('my_memory', size=4096)
Advertisement
Answer
In answer to question 2:
buffer[3:4]
is not, as you seem to suppose, an array reference. It is an expression that takes a slice of buffer
and assigns it to a new unnamed variable, which your function prints the ID of, then throws away. Then buffer[1:3]
does something similar and the new unnamed variable coincidentally gets allocated to the same memory location as the now disappeared copy of buffer[3:4]
, because Python’s garbage collection knew that location was free.
If you don’t throw away the slices after creating them, they will be allocated to different locations. Try this:
>>> b34 = buffer[3:4] >>> b13 = buffer[1:3] >>> b34 <memory at 0x0000024E65662940> >>> b13 <memory at 0x0000024E65662A00>
In this case they are at different locations because there are variables that refer to them both.
And both are at different locations to buffer
because they are all 3 different variables that are only related to one another by history, because of the way they were created.
Python variables are not blocks of raw memory that you can index into with a pointer, and thinking that you can access them directly as if they were C byte arrays is not helpful. What a particular Python interpreter does with your data deep down inside is generally not the business of a Python programmer. At the application level, why should anyone care where and how buffer[3:4]
is stored?
There is one good reason: if you have huge amounts of data, you may need to understand the details of the implementation because you have a performance problem. But generally the solution at the application level is to use modules like array
or pandas
where very clever people have already thought about those problems and come up with solutions.
What you are asking is not a question about Python, but about the the details of a particular CPython implementation. If you are interested in that sort of thing, and there is no reason why you should not be, you can always go and read the source. But that might be a little overambitious for the moment.