python - How to Handle Multithreading in loop

The below is the logic which is implemented.But here since it is infinite loop until and unless we break the loop the threads which are in the loop are unable to kill.Since these are in the loop the process memory is getting increased.How to kill a thread when function is completed.Is there any other procedure to implement this ?

 def sample(args):
       //Complex Functionality which it processes a image and stores in a folder

   def Camera():
     cap = cv2.VideoCapture(0)
     threads = []
     while(True):
         ret,frame = cap.read()
         t1= threading.Thread(target=sample, args=(frame,))
         t1.start()
         threads.append(t1)
         if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    for t2 in threads:
      t2.join()

I dont want to pause the main loop for the thread to complete the functionality

1 answer

  • answered 2017-11-12 20:23 soundstripe

    You can't kill threads in Python. If you use processes instead, you could kill those, but I'd recommend a different approach. Currently, you're assigning a brand new thread for every frame of video. Likely, what you are looking for is something more like this:

    from multiprocessing import Pool
    from multiprocessing.dummy import Pool as ThreadPool
    
    def sample(args):
        //Complex Functionality which it processes a image and stores in a folder
    
    def Camera():
        def allFrames():
            '''generator to grab all video frames'''
            cap = cv2.VideoCapture(0)
            while(True):
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break    
                ret,frame = cap.read()
                yield frame
    
        # this number should probably be number of logical processors - 1
        num_processes = 3
    
        workers = Pool(num_processes)
        # or 
        # workers = ThreadPool(num_processes)
        results = workers.map_async(sample, allFrames)
    
        print('all frames assigned to workers')