shared memory pointer

Tim tec at knology.net
Mon Sep 10 12:03:52 EDT 2007


On Sep 10, 10:11 am, Tim Golden <m... at timgolden.me.uk> wrote:
> Tim wrote:
> > Hello Everyone,
>
> > I am getting shared memory in python using the following.
>
> > szName = c_char_p(name)
> > hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE,
> >             None, PAGE_READONLY, 0, TABLE_SHMEMSIZE, szName)
> > if (hMapObject == 0):
> >             print "OpenKey: Could not open name file mapping object"
> >             raise WinError()
>
> > self.pData = windll.kernel32.MapViewOfFile(hMapObject,
> >        FILE_MAP_ALL_ACCESS, 0, 0, TABLE_SHMEMSIZE)
>
> Without answering your question directly, why not use
> the stdlib mmap module, which does exactly this for
> you behind the scenes?
>
> Docs -http://docs.python.org/lib/module-mmap.html
> Example -http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413807
>
> TJG

I reviewed the mmap function and I have a question. In the example
code below, what is the connection between the data in shared memory
and the mmap function. The fileno is zero. Why is it zero? The size
makes sense because there is 256 bytes in shared memory. The tag is
MyFileMappingObject. This tag does not have anything in common with
the shared memory. How can mmap point to any data in shared memory?
There is no coorelation.


pBuf = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
0, 0, SHMEMSIZE)
if (pBuf == 0):
    print "Could not map view of file"
    raise WinError()
else:
    memcpy = cdll.msvcrt.memcpy
    memcpy(pBuf, szMsg, len(szMsg))

shmem = mmap.mmap(0, 256, "MyFileMappingObject", mmap.ACCESS_WRITE)
shmem.write("Message from Python process")






More information about the Python-list mailing list