mmap Win9X bug?

Roberto Lopez-Gulliver gulliver at atr.co.jp
Wed Jan 16 03:37:44 EST 2002


Hi all,

I'm having problems with the mmap module under win98.

When I run the following file, the "reader" doesn't seem to get the
newly updated entry from the "writer" in the mmap-ed file.

BTW, the same code runs without problems under Win2000 and Linux.
I searched for somebody reporting the bug in c.l.py but found None!

Am I missing something stupid here? 

Thanks for any help.

--roberto

## -- begin ------------------------------------------------------------------
import mmap
SHM_NAME = "RTN_SHAREDMEMORY"
class SharedMemory :
    def __init__(self, key, size) :
        self.name = "%s_%d" % (SHM_NAME, key)
        mode = "r+" ## XXX don't miss the "+" in win32!!!
        f = open(self.name, mode)
        f.write("*"*size)
        self.shm = mmap.mmap(f.fileno(), size);
        f.close();
    def read(self, size, offset=0) :
        return self.shm[offset : offset+size] ## memcpy EXPENSIVE
    def write(self, data, offset=0) :
        size = len(data);
        self.shm[offset : offset+size] = data ## memcpy EXPENSIVE
    def __del__(self) :
        ## XXX TODO
        pass
    def detach(self) :
        ## XXX TODO
        pass

def main_shared() :
    import threading
    read, write = 1, 0
    tw = threading.Thread(target=main_shared_sub, args=(write,));
    tr = threading.Thread(target=main_shared_sub, args=(read,));
    tw.start();
    tr.start();

    ## wait for them to finish
    tw.join()
    tr.join()

def main_shared_sub(read) :
    import time
    key, size = 136, len("%f" % time.time())
    SLEEP = 0.1
    m = SharedMemory(key, size);

    count = 100
    while count :
        if read :
            s = m.read(size) ## [read] shared memory 
            print "<reader %5d> %s" % (count, s)
        else :
            s = "%f" % time.time()
            m.write(s) ## [write] shared memory 
            print "<writer %5d> %s" % (count, s)
        time.sleep(SLEEP);
        count -= 1;

if __name__ == "__main__" :
    main_shared()
    raw_input("Press any key to finish")
## -- end ------------------------------------------------------------------



More information about the Python-list mailing list