Home > database >  How to share a numpy array between multiple threads python?
How to share a numpy array between multiple threads python?

Time:05-13

I was actually trying to modify some yolov5 script. Here I'm trying to pass an array between threads.

def detection(out_q):
    while(cam.isOpened()):
        ref, img = cam.read()
        img = cv2.resize(img, (640, 320))

        result = model(img)
        yoloBbox = result.xywh[0].numpy() # yolo format
        bbox = result.xyxy[0].numpy() # pascal format
        for i in bbox:
            out_q.put(i) # 'i' is the List of length 6
        
def resultant(in_q):

    while(cam.isOpened()):
        ref, img =cam.read()
        img = cv2.resize(img, (640, 320))
        qbbox = in_q.get()
        print(qbbox)
if __name__=='__main__':
    q = Queue(maxsize = 10)

    t1 = threading.Thread(target= detection, args = (q, ))

    t2 = threading.Thread(target= resultant, args = (q, ))

    t1.start()
    t2.start()

    t1.join()
    t2.join()

I tried with this but it's giving me errors like:

Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:155

so is there any other method to pass the array? any kind of tutorial/ solution is appreciated. If there is any misunderstanding with my question, please let me know. Thanks a lot!!

CodePudding user response:

so is there any other method to pass the array?

Yes, you could use multiprocessing.shared_memory, it is part of standard library since python3.8, and PyPI has backport allowing to use it in python3.6 and python3.7. See example in linked docs to learn how to use multiprocessing.shared_memory with numpy.ndarray

CodePudding user response:

The answer provided by @Daweo suggesting use of shared memory is correct.

However, it's also worth considering using a lock to 'protect' access to the numpy array (which is not thread-safe).

See:- this

  • Related