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.