Skip to content

np.memmap works differently on windows and linux/mac #13338

@lstolcman

Description

@lstolcman

Hi. I have a big array (shapes 40k x 40k and bigger), and I want to use memmap and multiprocessing to fill it. I coded simple example to test it, and it does not work on windows. Expected result is to have values saved by each process (see screenshots).

Any suggestions?

Valid result (tested on virtualboxed xubuntu and MacOS):
obraz

Invalid result - array empty (Windows 7):
obraz

Reproducing code example:

import os
import numpy as np
import time
import multiprocessing as mp


def child(v, lock, m, p):
    np.random.seed(int.from_bytes(os.urandom(4), byteorder='little'))
    while True:
        vval = 0
        with lock:
            if v.value > 0:
                v.value -= 1
                vval = v.value
            else:
                break
        sl = np.random.rand()
        print(f'p{p} sleeping for {sl}')
        time.sleep(sl)
        m[0, vval] = p
        print(f'proc {p}: memmap[{vval}]={p} memmap={m}')



if __name__ == '__main__':
    memmap = np.memmap(
        'test1.memmap',
        dtype='int',
        mode='w+', shape=(1,10)
        )

    value = mp.Value('i', 10)
    lock = mp.Lock()
    procs = [mp.Process(
        target=child,
        args=(value, lock, memmap, i)
        ) for i in range(1,3)]
    for proc in procs:
        proc.start()
    for proc in procs:
        proc.join()
    
    print(f'after join, result:')
    print(memmap)

Numpy/Python version information:

windows: 1.16.2 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
linux: 1.16.2 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions