如何在python和C/C ++中使用共享内存 [英] How to use shared memory in python and C/C++

查看:210
本文介绍了如何在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:

  1. 共享内存::在C/C ++中,您可以使用 shmget shmat 之类的函数来获取指向共享的指针.记忆.用python处理此问题的等效方法是什么,以便python和C ++程序都可以使用同一块共享内存?

  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?

同步:由于这涉及多重处理,因此我们需要某种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

操作顺序如下:

  • 客户端进行 door_call()告诉守护程序创建共享内存区域
  • 守护程序安全地创建一个临时文件
  • 守护进程 open() s,然后 mmap() s该文件
  • 守护程序通过 door_return()
  • 将文件描述符传递回客户端
  • 客户端 mmap()创建文件描述符,并将结构中连续放置的变量与该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 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).

我们的客户利用图书馆来处理上述前5个步骤;该库随附了使用ctypes的Python包装器,以准确显示所需的函数和数据类型.

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 ++中使用共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆