bml1g12 / benchmarking_multiprocessing_np_array_comms

Benchmarking communication of numpy arrays between Python processes
MIT License
21 stars 5 forks source link

How to implement multi processes efficiently in my case? #5

Closed luan1412167 closed 3 years ago

luan1412167 commented 3 years ago

Hi @bml1g12 Thanks for your useful repo. My case is: I need to read frame from camera, then push it multiple processes to perform some huge tasks.

There are two approaches I can figure out:

Approach 1 : It is slow because push large image to queue is slow Approach 2 : other processes is very slow, so mp_array takes long time to release => main process can not share memory in the time => so slow

Can you suggest to me how to handle my case? Many thanks.

bml1g12 commented 3 years ago

Do all the processes need to read the frames at the same time? In my use case, I needed to ensure I was able to combine all the frames from the different cameras into one image from all the cameras, so I needed to ensure that each process grabbed one frame and only grabbed another frame after the shared memory lock has safely been released . I think It's true that releasing the shared memory lock can become a bottleneck in this approach, although it's still a lot faster than threading and serial approaches for the repo's example application.

But if in your application each process is doing operations that are completely unrelated to the other processes, then I think you can consider a slightly different architecture but also taking advantage of shared memory (to avoid needing to pickle the arrays into a queue).

One simple one if your processes are doing I/O bound work and not CPU bound would be to use threading and have threading.Queue for each array, this as fast because it does not pickle the array but has some problems listed at heading: "Threading to the rescue?"

If in your application you have one producer (camera) to many independent consumers (consumer) then one idea would be to make a separate shared memory pipeline for each process like the attached image, albeit I haven't experimented with this. image