classification
Title: Shared_Memory attaching to incorrect Address in Windows 10
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: OH, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2020-02-16 19:13 by OH, last changed 2020-02-16 21:31 by eryksun. This issue is now closed.

Messages (2)
msg362093 - (view) Author: Orwell (OH) Date: 2020-02-16 19:13
Shared Memory is attaching to incorrect memory location ,
ex : retried the documentation example.

>>> import numpy as np
>>> a = np.array([1, 1, 2, 3, 5, 8])
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory(create=True, size=a.nbytes)
>>> b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
>>> b[:] = a[:]
>>> b
array([1, 1, 2, 3, 5, 8])
>>> type(b)
<class 'numpy.ndarray'>
>>> type(a)
<class 'numpy.ndarray'>
>>> shm.name
'wnsm_62040dca'
>>> shm.buf
<memory at 0x000002AD10118DC0>

# In either the same shell or a new Python shell on the same machine

>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> existing_shm = shared_memory.SharedMemory(name='wnsm_62040dca')
>>> c = np.ndarray((6,), dtype=np.int64, buffer=existing_shm.buf)
>>> c
array([ 4294967297, 12884901890, 34359738373,           0,           0,
                 0], dtype=int64)
>>> c[-1]
0
>>> existing_shm.buf
<memory at 0x000001CF627E8DC0>
msg362105 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-02-16 21:31
shared_memory is not the problem here. Your example assumes that a.dtype is int64, but a numpy array defaults to "the minimum type required to hold the objects in the sequence", and the actual minimum depends on the size of the platform `long`. In Windows, a `long` is always 32-bit, regardless of whether the process is 32-bit or 64-bit. If [1, 1], [2, 3], and [5, 8]  are stored as int32 values, then we have the following values when unpacked as 64-bit:

    >>> int.from_bytes(b'\x01\x00\x00\x00\x01\x00\x00\x00', 'little')
    4294967297
    >>> int.from_bytes(b'\x02\x00\x00\x00\x03\x00\x00\x00', 'little')
    12884901890
    >>> int.from_bytes(b'\x05\x00\x00\x00\x08\x00\x00\x00', 'little')
    34359738373
History
Date User Action Args
2020-02-16 21:31:24eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg362105

resolution: not a bug
stage: resolved
2020-02-16 19:24:12OHsettitle: Shared_Memory attaching to incorrect Address in Windows -> Shared_Memory attaching to incorrect Address in Windows 10
2020-02-16 19:13:46OHcreate