This is a more general question regarding concurrency/parallelism, but as I don't have too much experience in those topics I am asking specifically for my use case.
I have two cameras plugged into my PC. I can control them over an SDK (PySpin in my case). I acquire and save the images for duration seconds using this simplified loop:
count = 0
t_end = time.time() + duration
while time.time() < t_end:
for cam in cameras:
img_raw = cam.GetNextImage()
img = img_raw.GetNDArray()
cv2.imwrite('{}_img_{}.png'.format(device_name, count), img)
img_raw.Release()
count += 0
There are two problems here:
- The cameras block each other. Let's say the first camera has a high exposure time. The other cameras would need to wait for this camera to take the image.
- Saving the images is slowing down the process and could theoretically slow down the frame rate of the cameras.
I would like to do following: The cameras should take the images in parallel and images should be saved in whenever an image is taken, i.e. while the camera is already acquiring the next image.
As I said, I am not familiar with concurrency/parallelism and therefore I don't really know how to tackle it. But, I think to solve the first problem I need multi processing/threading (parallelism) for example using multiprocessing. But for the second problem I would need an asynchronous process using asyncio.
Am I on the right path? If so, how could I implement this?