0

I would like to create and write an image to the memory in such a way that other processes are able to read it. The goal is to have a Python script create some graph using matplotlib.pyplot, write it to the memory as a png file and then have a Qt application written in C++ or Python read that image and be able to render it in it's own window using the capabilities of Qt. I would specifically want to avoid saving the image to the hard disk.

IF I was to use the hard disk as a bridge between the two processes, my code would look something like this:
Python script to create the image and save it as a png:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y, marker = "o", linestyle = "-")
plt.savefig("my_graph.png")

Qt Application written in Python that has a GraphWidget class to render it:

class GraphWidget(QWidget):
    def __init__(self, parent : parent : Optional[QWidget] = None) -> None:
        super().__init__(parent)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0, 0, QPixmap("./my_graph.png"))

In this example, the Qt application is written in Python because at the time of writing this it is more convenient to provide an example in Python (I will update the question with C++ code as soon as possible), but the point is the same. This code uses the hard disk as a means to share the image between the two processes. Is there a way to achieve the same result without utilizing the disk and writing the image straight to memory using Python and reading it from the memory with C++?

If this can't be done by utilizing the memory, can you suggest any alternatives to achieve the same result without using the hard disk?

I am a beginner programmer and this is my first question in years, so any help would be appreciated that could expand my view of possible solutions.

10
  • 2
    You need to ask the O.S. for an area of memory that it won't touch. Most of the memory is used for swapping programs and things. Files would be good, also read up on sockets. Commented Jul 8 at 17:03
  • 2
    Search the Qt libraries to see if they have any API for shared memory. Commented Jul 8 at 17:04
  • 1
    I am using Windows 10. My consideration for trying to avoid the disk is that the Qt application and the Python scripts responsible for generating the images are part of a single product and my supervisor on this University project suggested it would be crude to use the disk for sharing an image between two parts of the same software and suggested in-memory sharing, but I didn't recieve any pointers as to what my options could be and where to begin research.
    – Falesz
    Commented Jul 8 at 17:09
  • 2
    you can use multiprocessing.shared_memory, it uses CreateFileMappingA on windows, so you can call that from C++ side, still i don't think the added complexity is justified.
    – Ahmed AEK
    Commented Jul 8 at 17:14
  • 2
    Simplest way with Win10/11 might be to create a RAM drive which acts as if it was a HDD. Lots of info available on how to do this. Commented Jul 8 at 17:34

0