I have 2 steps in processing pipeline which runs over many images:
- Step 1: Load locally (or download) image (IO bound)
- Step 2: Run machine learning model (CPU/ GPU/ Compute bound/ single threaded because the model is big). How do I limit the number of images stored in memory (from step 1) queuing for the 2nd step. This is called backpressure in Reactive programming.
Without backpressure, all the work from Step 1 might pile up, leading to a high memory usage just for having images open.
I guess I could use a semaphore (e.g. of 5) which represents roughly the amount of memory I am willing to give for step 1 (5 pictures). I guess this would make 5 of my background threads to block, which is probably a bad thing? (that's a serious question: is it bad to block a background thread, since it consumes resources.)