0

Hello I wanted to know how to manage threads in python. Let say I have a function iterated multiple times from a list, using threading like this:

import threading

thread_list = []
maxthreads = 5 #lets say I want max 5 concurrent threads
threadcount = 0

def my_func(arg):
    #do stuff
for x in mylist:
    t = threading.Thread(name='Test {}'.format(x), target=my_func, args=[x])
    t.start()
    thread_list.append(t)
    threadcount += 1
    if threadcount >= maxthreads:
        for thread in thread_list:
            thread.join()
        threadcount=0

The code works, but I have to wait for all threads to complete before lunching the next five. What I want to achieve is, whenever one thread is completed, start a new one, so in this example, whenever the script goes from 5 to 4 active threads (becouse one is completed) it starts a new one and keeps operating again with 5 threads.

I've read about concurrent.futures and ThreadExecutor but dont know how to implement in this logic.

mindmod
  • 11
  • 1
  • You want something called a Pool: [This](https://stackoverflow.com/questions/3033952/threading-pool-similar-to-the-multiprocessing-pool) might help. – MegaIng Jun 12 '21 at 14:02

0 Answers0