The previous blog said how to implement inter-process communication through named pipes, but to use a named pipe in Windows, it would be too cumbersome to use Python to investigate Windows APIs, and then think about how it could be done by sharing memory. Check it out, Python can use the Mmap module to implement this function.
The Mmap module in Python implements shared memory by mapping the same common file. After the file is mapped to the process address space, the process can access the file as if it were accessing memory.
However, Mmap has a slightly different API on Linux and Windows, with details to view Mmap's documentation.
Let's look at an example:
server.py
This program uses the Test.dat file to map the memory, and allocates 1024 bytes of size, updating the memory information every second.
Import MmapImport ContextlibImport timeWith open ( "Test.dat", "W") as f:f.write ( Span class= "hljs-string" > ' \x00 ' * 1024) with Open ( ' Test.dat ', ' r+ ') as F: with contextlib.closing (Mmap.mmap (F.fileno (), 1024, access= Mmap. access_write)) as m: for i in range (1, 10001): M.seek (0) s = "msg" + str (i) s.rjust (1024, ' \x00 ') m.write (s) M.flush () time.sleep (1)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
client.py
This program loads data into memory from the file Test.dat mapped above.
import Mmapimport contextlibimport time while true: with open ( ' Test.dat ', ' R ') as F: with contextlib.closing (Mmap.mmap (F.fileno (), 1024, access= Mmap. Access_read)) as m:s = M.read (1024). Replace ( ' \x00 ', " print s Time.sleep (1)
The code above can be run on Linux and Windows because we explicitly specified using the Test.dat file to map the memory. If we only need to implement shared memory on Windows, we can simplify the above code by specifying a tagname to identify it without specifying the file to use. As follows:
server.py
import mmapimport contextlibimport timewith contextlib.closing(mmap.mmap(-1, 1024, tagname=‘test‘, access=mmap.ACCESS_WRITE)) as m: for i in range(1, 10001): m.seek(0) m.write("msg " + str(i)) m.flush() time.sleep(1)
client.py
import mmapimport contextlibimport timewhile True: with contextlib.closing(mmap.mmap(-1, 1024, tagname=‘test‘, access=mmap.ACCESS_READ)) as m: s = m.read(1024).replace(‘\x00‘, ‘‘) print s time.sleep(1)
Python shared memory for interprocess communication