Running python multiprocess for Image processing

后端 未结 2 1332
温柔的废话
温柔的废话 2021-01-27 18:37

I have a python function that takes in an image path and outputs true or false depending whether the image is black or not. I want to process several images on the same machine

2条回答
  •  一生所求
    2021-01-27 19:30

    I would suggest looking at Pools to easily create processes on the fly. If you need to have some shared state, in this case a boolean indicating a non-black image has been found, look at Managers.

    Update: Here is an example of what I mean.

    import multiprocessing.Manager as Manager
    import multiprocessing.Pool as Pool
    
    m = Manager()
    p = Pool(processes=5)
    
    state_info = m.dict()
    state_info['image_found'] = False
    
    def processImage(img):
    
        # ... Process Image ...
    
        if imageIsBlack(img):
            state_info['image_found'] = True
            p.terminate()
    
     p.apply(processImage, imageList)
    
     if state_info['image_found']:
         print 'There was a black image!!'
     else:
         print 'No black images were found.'
    

提交回复
热议问题