1

I am writing a multiprocessing system in python. One of the child processes is in charge of reading frames from a camera stream using cv2 and pass that frame along to another child process for some manipulation and previewing. The problem is that in order to pass messages from one process to another I use the 'pickle' module to serialize the message objects. Time is crucial here so instead of using a numpy array for each frame which would get serialized I am using numpy.memmap. What I'm trying to understand is how python would handle the memory created by memmap. What would happen once the stream reader object no longer keeps a reference to the memmap object (after it is pickled and sent). Could the frame be released from memory? If not then am I facing a memory issue? How would python know when the frame is no longer in use and delete it?

Some sample code:

import cv2
import numpy as np
from multiprocessing import Queue, Process
import pickle


def second_child_process(queue):

    while True:
        pickled_frame = queue.get()
        fp = pickle.loads(pickled_frame)
        cv2.imshow("Video Window", fp)
        cv2.waitKey(33)


def first_child_process(queue):
    ret = True
    vc = cv2.VideoCapture("/dev/0")
    while (ret):

        memmap_p = np.memmap("/dev/zero", dtype='uint8', mode='w+', shape=(360, 640, 3))
        ret = vc.read(memmap_p)

        p = pickle.dumps(memmap_p)
        queue.put(p)


if __name__ == '__main__':
    queue = Queue(10)
    process1 = Process(target=first_child_process, args=(queue,))
    process2 = Process(target=second_child_process, args=(queue,))

    process1.start()
    process2.start()
    process1.join()

Running this code doesn't show any memory issues. Still I would like to understand why.

royeet
  • 829
  • 1
  • 9
  • 12

0 Answers0