2

I'm attempting to have image processing done using multiple processes, to improve speed/efficiency, but it's always giving me a broken pipe error. even with the code

import cv2
import numpy as np
from multiprocessing import Process, freeze_support
def findRed(img, pipe):
    x = 5

frame = cv2.imread("test.jpg")
test = Process(target=findRed, args=(frame, 5))
test.start()
test.join()

if I instead set frame equal to something not using imread, it doesn't give me the broken pipe error. anybody know why this happens, or how to fix it?

Tyler Silva
  • 411
  • 6
  • 14

1 Answers1

0

A new process doesn't share memory space and handlers to system resources with other ones except special allocated shared memory regions, semaphores, mutexes etc. So if a frame object includes handlers, like to the PIPE object, it won't be valid/accessible in the new one. That's how I understand it. According to Python multiprocessing, passing an object reference containig a semaphore passing an object leads to object copying through the PIPE which might be related.

In general I would advise against passing complex objects to the process, they might be passed improperly.

The solution would be to pass "simple" values, like numbers strings etc. In your case, you can pass a file name to the process which will in turn read a file and process it itself.

Andrew_Lvov
  • 4,621
  • 2
  • 25
  • 31
  • 1
    oh, that makes sense, i was planning on passing a portion of the image, but i can instead just pass a range for it to look through – Tyler Silva Sep 10 '18 at 13:33
  • but does the `frame` object contains handlers? isn't that just the image contents? – Jean-François Fabre Sep 10 '18 at 14:30
  • @Jean-FrançoisFabre it depends. I don't know the internals, but theoretically it may be lazy loading and hold a reference to an opened file and current read position. At least I might be doing such an implementation if I was the author for very large images – Andrew_Lvov Sep 10 '18 at 16:11