How to use shared memory in python and C/C++(如何在 python 和 C/C++ 中使用共享内存)
问题描述
我正在尝试修改 python 程序,以便能够使用共享内存与 C++ 程序进行通信.python程序的主要职责是从位于共享内存中的输入队列中读取一些视频帧,对视频帧执行一些操作并将其写回共享内存中的输出队列.
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:
共享内存: 在 C/C++ 中,您可以使用
shmget
和shmat
等函数来获取指向共享内存的指针记忆.在 python 中处理这个问题的等效方法是什么,以便 python 和 C++ 程序可以使用同一块共享内存?
Shared memory: In C/C++, you can use functions like
shmget
andshmat
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?
同步:因为这涉及到多处理,我们需要某种锁定机制用于 C++ 和 python 程序中的共享内存.我如何在 python 中做到这一点?
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?
非常感谢!
推荐答案
也许 shmget
和 shmat
不一定是最适合您使用的接口.在我从事的一个项目中,我们使用内存映射的文件通过 C 和 Python API 提供对守护进程的访问,这为我们提供了一种访问数据的非常快速的方法
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:
- 客户端发出
door_call()
告诉守护进程创建共享内存区域 - 守护进程安全地创建一个临时文件
- 守护进程
open()
s 然后mmap()
s 那个文件 - 守护进程通过
door_return()
将文件描述符传递回客户端 - 客户端
mmap()
s 文件描述符并将结构中连续放置的变量与该 fd 相关联 - 客户端在需要时对这些变量执行所需的任何操作.
- 守护进程从共享区域读取数据并进行自己的更新(在我们的例子中,将来自该共享区域的值写入日志文件).
- 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 thenmmap()
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).
我们的客户使用库来处理上述前 5 个步骤;该库带有 Python 包装器,使用 ctypes 来准确公开所需的函数和数据类型.
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.
对于您的问题空间,如果只是写入输出队列的 python 应用程序,那么您可以跟踪仅在 python 应用程序中处理了哪些帧.如果您的 python 和 c++ 应用程序都在写入输出队列,那么这会增加您的难度,也许重构整个应用程序架构将是一项不错的投资.
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.
这篇关于如何在 python 和 C/C++ 中使用共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 python 和 C/C++ 中使用共享内存
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01