Named shared memory between C++ and python on Windows

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

Named shared memory between C++ and python on Windows

问题

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

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

  1. #include <boost/interprocess/shared_memory_object.hpp>
  2. #include <boost/interprocess/mapped_region.hpp>
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace boost::interprocess;
  6. #define SHM_NAME "SharedMemory"
  7. #define BYTES_NUMBER 541798 // 图像大小为529 KB
  8. int main()
  9. {
  10. shared_memory_object shm(open_or_create, SHM_NAME, read_write);
  11. shm.truncate(BYTES_NUMBER);
  12. mapped_region region(shm, read_write);
  13. std::ifstream imageFile("input.bmp", std::ios::binary);
  14. if (!imageFile)
  15. {
  16. std::cerr << "无法打开图像文件。" << std::endl;
  17. return 1;
  18. }
  19. std::copy(std::istreambuf_iterator<char>(imageFile), std::istreambuf_iterator<char>(), static_cast<char*>(region.get_address()));
  20. imageFile.close();
  21. std::cout << "图像成功写入共享内存。" << std::endl;
  22. // 启动Python脚本
  23. std::string command = "python sharedMemory.py";
  24. std::system(command.c_str());
  25. std::cout << "按任意键退出。" << std::endl;
  26. _getch();
  27. return 0;
  28. }

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

  1. import mmap
  2. from PIL import Image
  3. SHM_NAME = "SharedMemory"
  4. BYTES_NUMBER = 541798
  5. IMAGE_WIDTH = 511
  6. IMAGE_HEIGHT = 265
  7. if __name__ == "__main__":
  8. shm = mmap.mmap(-1, BYTES_NUMBER, SHM_NAME)
  9. image_data = shm.read(BYTES_NUMBER)
  10. shm.close()
  11. image = Image.frombytes("RGB", (IMAGE_WIDTH, IMAGE_HEIGHT), image_data, "raw")
  12. 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:

  1. #include &lt;boost/interprocess/shared_memory_object.hpp&gt;
  2. #include &lt;boost/interprocess/mapped_region.hpp&gt;
  3. #include &lt;iostream&gt;
  4. #include &lt;fstream&gt;
  5. using namespace boost::interprocess;
  6. #define SHM_NAME &quot;SharedMemory&quot;
  7. #define BYTES_NUMBER 541798 // Image size is 529 KB
  8. int main()
  9. {
  10. shared_memory_object shm(open_or_create, SHM_NAME, read_write);
  11. shm.truncate(BYTES_NUMBER);
  12. mapped_region region(shm, read_write);
  13. std::ifstream imageFile(&quot;input.bmp&quot;, std::ios::binary);
  14. if (!imageFile)
  15. {
  16. std::cerr &lt;&lt; &quot;Failed to open the image file.&quot; &lt;&lt; std::endl;
  17. return 1;
  18. }
  19. std::copy(std::istreambuf_iterator&lt;char&gt;(imageFile), std::istreambuf_iterator&lt;char&gt;(), static_cast&lt;char*&gt;(region.get_address()));
  20. imageFile.close();
  21. std::cout &lt;&lt; &quot;Image successfully written to shared memory.&quot; &lt;&lt; std::endl;
  22. // Launch Python script
  23. std::string command = &quot;python sharedMemory.py&quot;;
  24. std::system(command.c_str());
  25. std::cout &lt;&lt; &quot;Press any key to exit.&quot; &lt;&lt; std::endl;
  26. _getch();
  27. return 0;
  28. }

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

  1. import mmap
  2. from PIL import Image
  3. SHM_NAME = &quot;SharedMemory&quot;
  4. BYTES_NUMBER = 541798
  5. IMAGE_WIDTH = 511
  6. IMAGE_HEIGHT = 265
  7. if __name__ == &quot;__main__&quot;:
  8. shm = mmap.mmap(-1, BYTES_NUMBER, SHM_NAME)
  9. image_data = shm.read(BYTES_NUMBER)
  10. shm.close()
  11. image = Image.frombytes(&quot;RGB&quot;, (IMAGE_WIDTH, IMAGE_HEIGHT), image_data, &quot;raw&quot;)
  12. 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

这是你的翻译:

  1. 我注意到你的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_READACCESS_WRITEACCESS_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:

确定