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

前端 未结 2 2041
迷失自我
迷失自我 2020-12-31 20:42

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

2条回答
  •  误落风尘
    2020-12-31 21:31

    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.

提交回复
热议问题