Warm tip: This article is reproduced from serverfault.com, please click

How to use shared memory in python and C/C++

发布于 2018-03-05 04:57:21

I am trying to modify a python program to be able to communicate with a C++ program using shared memory. The main responsibility of the python program is to read some video frames from an input queue located in shared memory, do something on the video frame and write it back to the output queue in shared memory.

I believe there are few things I need to achieve and it would be great if someone can shed some light on it:

  1. Shared memory: In C/C++, you can use functions like shmget and shmat to get the pointer to the shared memory. What is the equivalent way to handle this in python so both python and C++ program can use the same piece of shared memory?

  2. Synchronization: Because this involves multi-processing, we need some sort of locking mechanism for the shared memory in both C++ and python programs. How can I do this in python?

Many thanks!

Questioner
HeiHei
Viewed
0
James McPherson 2018-03-06 03:31:32

Perhaps shmget and shmat are not necessarily the most appropriate interfaces for you to be using. In a project I work on, we provide access to a daemon via a C and Python API using memory mapped files, which gives us a very fast way of accessing data

The order of operations goes somewhat like this:

  • the client makes a door_call() to tell the daemon to create a shared memory region
  • the daemon securely creates a temporary file
  • the daemon open()s and then mmap()s that file
  • the daemon passes the file descriptor back to the client via door_return()
  • the client mmap()s the file descriptor and associates consecutively-placed variables in a structure with that fd
  • the client does whatever operations it needs on those variables - when it needs to do so.
  • the daemon reads from the shared region and does its own updates (in our case, writes values from that shared region to a log file).

Our clients make use of a library to handle the first 5 steps above; the library comes with Python wrappers using ctypes to expose exactly which functions and data types are needed.

For your problem space, if it's just the python app which writes to your output queue then you can track which frames have been processed just in the python app. If both your python and c++ apps are writing to the output queue then that increases your level of difficulty and perhaps refactoring the overall application architecture would be a good investment.