shared memory pointer

Tim Golden mail at timgolden.me.uk
Mon Sep 10 12:25:38 EDT 2007


Tim wrote:
> 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")

Your question seems a little strange. You appear to be creating
a memory mapping via ctypes and then expecting the Python mmap
module to know about it. Of course it doesn't. What you do is
to create two mmap mappings with the same tag name which --
under the covers -- point to the same shared memory. I just
tried it ad-hoc in two interpreters and it works fine.

I admit I am absolutely no expert on this area. My initial
suggestion was simply because your question triggered a
memory of what I'd seen elsewhere. Have a look at this
thread from a few years ago to see if it clarifies:

   http://www.thescripts.com/forum/thread25421.html

(although what it -- and your question -- clearly do clarify
is a lack of clarity in the documentation!)

TJG



More information about the Python-list mailing list