如何使用 c++ boost::进程间库通过命名共享内存与Python通信?

发布于 2025-01-13 13:36:52 字数 1441 浏览 3 评论 0 原文

目的:让c++进程与本机上的python进程进行通信,打算使用共享内存进行通信。 C++使用boost::interprocess库通过shared_memory_object创建命名共享内存,并向其中写入数据,而python则读取相应的数据。

问题:python如何根据c++中对应共享内存的名称来访问数据?

这是我写的剪辑:

          struct test_datas {
            char name[128];
            char sender[128];
            union u_data {
                char s1_[1024];
                long l1_;
                double d_;
            } u_member;
        };
        
        void write_shared_memory() {
            using namespace boost::interprocess;
            shared_memory_object shdmem(open_or_create , "monika", read_write);
            shdmem.truncate(sizeof(test_datas));
            mapped_region region(shdmem , read_write);
        
            auto *data_ptr = static_cast<test_datas *>(region.get_address());
            sprintf(data_ptr->name , "%s" , "named");
            sprintf(data_ptr->sender , "%s" , "myself");
            data_ptr->u_member.d_ = 66.890;
    }

这是相应的Python代码:

    from multiprocessing import shared_memory
    
    if __name__ == "__main__":
        shm_a = shared_memory.SharedMemory(name="monika", create=False)
        buffer = shm_a.buf
        print(buffer[0])

    error: FileNotFoundError: [WinError 2] The system cannot find the file specified: 'monika'

Purpose: Let c++ process communicate with python process on this computer, and intend to use shared memory to communicate.
C++ uses the boost::interprocess library to create a named shared memory through shared_memory_object, and writes data into it, while python reads the corresponding data.

Question: how does python access data based on the name of the corresponding shared memory in c++?

Here's the clip I wrote:

          struct test_datas {
            char name[128];
            char sender[128];
            union u_data {
                char s1_[1024];
                long l1_;
                double d_;
            } u_member;
        };
        
        void write_shared_memory() {
            using namespace boost::interprocess;
            shared_memory_object shdmem(open_or_create , "monika", read_write);
            shdmem.truncate(sizeof(test_datas));
            mapped_region region(shdmem , read_write);
        
            auto *data_ptr = static_cast<test_datas *>(region.get_address());
            sprintf(data_ptr->name , "%s" , "named");
            sprintf(data_ptr->sender , "%s" , "myself");
            data_ptr->u_member.d_ = 66.890;
    }

Here is the corresponding Python code:

    from multiprocessing import shared_memory
    
    if __name__ == "__main__":
        shm_a = shared_memory.SharedMemory(name="monika", create=False)
        buffer = shm_a.buf
        print(buffer[0])

    error: FileNotFoundError: [WinError 2] The system cannot find the file specified: 'monika'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

完美的未来在梦里 2025-01-20 13:36:52

假设这里使用 POSIX(例如 Linux),两者都会创建 SHM“文件”,所以说 Ubuntu /dev/shm/monika。

然而,Python 默认情况下似乎会尝试清理它。参见文档

当一个进程不再需要访问其他进程可能仍需要的共享内存块时,应调用 close() 方法。

然而,这表明Python共享内存设施的通用性远不如平常,请参见退出时删除的共享内存

Assuming POSIX (e.g. Linux) here, both create SHM "files", so on say Ubuntu /dev/shm/monika.

However, it seems that Python by default attempts to clean it up. See docs:

When one process no longer needs access to a shared memory block that might still be needed by other processes, the close() method should be called.

However, this suggests that the Python shared memory facility is much less general purpose than usual, see e.g. Shared memory deleted at exit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文