SoFunction
Updated on 2024-10-26

Python Interprocess Communication: Shared Memory Explained

The previous blog said how to realize inter-process communication through named pipes, but to use named pipes in windows, you need to use python to investigate the windows api, which is too troublesome, so I thought it is not possible to realize it through the way of shared memory. After checking, Python can use mmap module to realize this function.

The mmap module in Python implements shared memory by mapping the same ordinary file. Once the file is mapped into the process address space, the process can access the file as if it were memory.

However, mmap has a slightly different API on linux and windows, check the mmap documentation for details.

See an example below:

This program uses a file to map memory and allocates a size of 1024 bytes, updating the memory information every second.

import mmap
import contextlib
import time

with open("", "w") as f:
  ('\x00' * 1024)

with open('', 'r+') as f:
  with (((), 1024, access=mmap.ACCESS_WRITE)) as m:
    for i in range(1, 10001):
      (0)
      s = "msg " + str(i)
      (1024, '\x00')
      (s)
      ()
      (1)

This program loads data into memory from the file mapped above.

import mmap
import contextlib
import time

while True:
  with open('', 'r') as f:
    with (((), 1024, access=mmap.ACCESS_READ)) as m:
      s = (1024).replace('\x00', '')
      print s
  (1)

The above code works on both linux and windows because we explicitly specify the use of a file to map the memory. If we need to implement shared memory on windows only, instead of specifying the file to use, we can identify it by specifying a tagname, so we can simplify the above code. As follows:

import mmap
import contextlib
import time

with ((-1, 1024, tagname='test', access=mmap.ACCESS_WRITE)) as m:
  for i in range(1, 10001):
    (0)
    ("msg " + str(i))
    ()
    (1)

import mmap
import contextlib
import time

while True:
  with ((-1, 1024, tagname='test', access=mmap.ACCESS_READ)) as m:
    s = (1024).replace('\x00', '')
    print s
  (1)

This is the whole content of this article.