As an exercise, I try to implement a non blocking mechanism to manage my own pool of threads (I know concurrent.futures). I ended with a problem: threads are always alive, even after they have completed.
I have written this little code to expose the problem.
import threading
import time
class myThread (threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num + 1
def run(self):
print(f">>> {self.name} is starting")
time.sleep(self.num*2.0)
print(f"<<< {self.name} is finishing")
num_threads = 3
threads = []
for i in range(num_threads):
t = myThread(i)
t.start()
threads.append(t)
num_finished_threads = 0
while num_finished_threads < num_threads:
time.sleep(0.5)
for t in threads:
if t.is_alive:
print(f"{t.name} is alive")
else:
t.join()
num_finished_threads += 1
Can anyone explain me the behaviour of python: why those threads are still alive after completion ?
>>> Thread-1 is starting
>>> Thread-2 is starting
>>> Thread-3 is starting
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
<<< Thread-1 is finishing
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
<<< Thread-2 is finishing
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
<<< Thread-3 is finishing
Thread-1 is alive
Thread-2 is alive
Thread-3 is alive
Thanks a lot.