Named shared memory between C++ and python on Windows

huangapple go评论74阅读模式
英文:

Named shared memory between C++ and python on Windows

问题

我正在开发一个C++应用程序,通过在Windows上使用命名共享内存与Python脚本交换数据。我面临的问题是,Python脚本显示一幅完全黑色的图像,就好像它从内存中读取了全零。我怀疑代码可能存在问题。

在C++应用程序中,我正在使用Boost.Interprocess库进行共享内存操作。以下是代码:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <fstream>

using namespace boost::interprocess;
#define SHM_NAME "SharedMemory"
#define BYTES_NUMBER 541798 // 图像大小为529 KB

int main()
{
    shared_memory_object shm(open_or_create, SHM_NAME, read_write);
    shm.truncate(BYTES_NUMBER);

    mapped_region region(shm, read_write);

    std::ifstream imageFile("input.bmp", std::ios::binary);
    if (!imageFile)
    {
        std::cerr << "无法打开图像文件。" << std::endl;
        return 1;
    }

    std::copy(std::istreambuf_iterator<char>(imageFile), std::istreambuf_iterator<char>(), static_cast<char*>(region.get_address()));
    imageFile.close();
    std::cout << "图像成功写入共享内存。" << std::endl;

    // 启动Python脚本
    std::string command = "python sharedMemory.py";
    std::system(command.c_str());

    std::cout << "按任意键退出。" << std::endl;
    _getch();

    return 0;
}

在Python脚本中,我使用了mmap模块从共享内存中读取图像数据。以下是脚本:

import mmap
from PIL import Image

SHM_NAME = "SharedMemory"
BYTES_NUMBER = 541798
IMAGE_WIDTH = 511
IMAGE_HEIGHT = 265

if __name__ == "__main__":
    shm = mmap.mmap(-1, BYTES_NUMBER, SHM_NAME)
    image_data = shm.read(BYTES_NUMBER)
    shm.close()

    image = Image.frombytes("RGB", (IMAGE_WIDTH, IMAGE_HEIGHT), image_data, "raw")
    image.show()

运行代码时,Python脚本显示的图像完全是黑色的。我怀疑问题可能与我如何在共享内存中写入/读取图像数据有关。

英文:

I'm working on a C++ application that exchanges data with a Python script through named shared memory on Windows. I'm facing a problem where the Python script displays a completely black image as if it's reading all zeros from the memory. I suspect there might be an issue with the code.

In the C++ application, I'm using Boost.Interprocess library for shared memory operations. Here's the code:

#include &lt;boost/interprocess/shared_memory_object.hpp&gt;
#include &lt;boost/interprocess/mapped_region.hpp&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;

using namespace boost::interprocess;
#define SHM_NAME &quot;SharedMemory&quot;
#define BYTES_NUMBER 541798 // Image size is 529 KB

int main()
{
    shared_memory_object shm(open_or_create, SHM_NAME, read_write);
    shm.truncate(BYTES_NUMBER);

    mapped_region region(shm, read_write);

    std::ifstream imageFile(&quot;input.bmp&quot;, std::ios::binary);
    if (!imageFile)
    {
        std::cerr &lt;&lt; &quot;Failed to open the image file.&quot; &lt;&lt; std::endl;
        return 1;
    }

    std::copy(std::istreambuf_iterator&lt;char&gt;(imageFile), std::istreambuf_iterator&lt;char&gt;(), static_cast&lt;char*&gt;(region.get_address()));
    imageFile.close();
    std::cout &lt;&lt; &quot;Image successfully written to shared memory.&quot; &lt;&lt; std::endl;

    // Launch Python script
    std::string command = &quot;python sharedMemory.py&quot;;
    std::system(command.c_str());

    std::cout &lt;&lt; &quot;Press any key to exit.&quot; &lt;&lt; std::endl;
    _getch();

    return 0;
}

In the Python script, I'm using the mmap module to read the image data from shared memory. Here's the script:

import mmap
from PIL import Image

SHM_NAME = &quot;SharedMemory&quot;
BYTES_NUMBER = 541798
IMAGE_WIDTH = 511
IMAGE_HEIGHT = 265

if __name__ == &quot;__main__&quot;:
    shm = mmap.mmap(-1, BYTES_NUMBER, SHM_NAME)
    image_data = shm.read(BYTES_NUMBER)
    shm.close()

    image = Image.frombytes(&quot;RGB&quot;, (IMAGE_WIDTH, IMAGE_HEIGHT), image_data, &quot;raw&quot;)
    image.show()

When running the code, the image displayed by the Python script appears completely black. I suspect that the issue might be related to how I'm writing/reading the image data on the shared memory.

答案1

得分: 2

这是你的翻译:

我注意到你的Python脚本中的第10行。当我运行你的代码时,我的IDE告诉我第三个参数无效,因为它是一个字符串 `shm = mmap.mmap(-1, BYTES_NUMBER, SHM_NAME)`。我建议将其更改为类似这样的表达式 `shm = mmap.mmap(-1, BYTES_NUMBER, access=mmap.ACCESS_READ, offset=0)`。access参数有三个选项:ACCESS_READ,ACCESS_WRITE,ACCESS_COPY。尝试调整该参数并查看是否有所变化。通常在我们想要从起始地址映射文件时,会将offset参数指定为0。参考资料:<https://www.askpython.com/python-modules/mmap-function>,<https://docs.python.org/3/library/mmap.html>
英文:

This first thing I notice in your python script is line 10. When I run your code, my IDE tells me that the third argument is invalid because it is a string
shm = mmap.mmap(-1, BYTES_NUMBER, SHM_NAME). I would change it to something like shm = mmap.mmap(-1, BYTES_NUMBER, access=mmap.ACCESS_READ, offset=0). The access parameter has three options: ACCESS_READ, ACCESS_WRITE, ACCESS_COPY. Try playing around with that parameter and see if that changes anything. The offset argument is often specified as 0 when we want to map the file from the start address. References: <https://www.askpython.com/python-modules/mmap-function>, <https://docs.python.org/3/library/mmap.html>

huangapple
  • 本文由 发表于 2023年7月6日 20:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定