Python shared memory for inter-process communication

Source: Internet
Author: User

Python shared memory for inter-process communication

The previous blog talked about how to implement inter-process communication through the naming pipeline, but to use the naming pipeline in windows, it is too troublesome to use python to investigate the windows api, so I thought whether it could be implemented through shared memory. Check that the mmap module can be used in Python to implement this function.

The mmap module in Python implements shared memory by ing the same common file. After the file is mapped to the process address space, the process can access the file as it accesses the memory.

However, mmap APIs on linux and windows are somewhat different. For details, refer to mmap documentation.

The following is an example:

Server. py

This program uses the test. dat file to map memory and allocate a size of 1024 bytes. It updates the memory information every second.

import mmapimport contextlibimport timewith open("test.dat", "w") as f:  f.write('\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)

Client. py

This program loads data from the above mapped file test. dat to the memory.

import mmapimport contextlibimport timewhile 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 above code can be run on linux and windows, because we explicitly specify the use of the test. dat file to map memory. If we only need to implement shared memory on windows, we can identify it by specifying a tagname instead of specifying the file to be used, so we can simplify the above Code. 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)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.