Python shared memory for interprocess communication

Source: Internet
Author: User

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)              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.