Checking on a thread / remove from list Checking on a thread / remove from list multithreading multithreading

Checking on a thread / remove from list


As TokenMacGuy says, you should use thread.is_alive() to check if a thread is still running. To remove no longer running threads from your list you can use a list comprehension:

for t in my_threads:    if not t.is_alive():        # get results from thread        t.handled = Truemy_threads = [t for t in my_threads if not t.handled]

This avoids the problem of removing items from a list while iterating over it.


you need to call thread.isAlive()to find out if the thread is still running


mythreads = threading.enumerate()

Enumerate returns a list of all Thread objects still alive.https://docs.python.org/3.6/library/threading.html