英文:
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 <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 // 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("input.bmp", std::ios::binary);
if (!imageFile)
{
std::cerr << "Failed to open the image file." << 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 << "Image successfully written to shared memory." << std::endl;
// Launch Python script
std::string command = "python sharedMemory.py";
std::system(command.c_str());
std::cout << "Press any key to exit." << 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 = "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()
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论