python – Checking on a thread / remove from list

python – 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 = True
my_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

python – Checking on a thread / remove from list

mythreads = threading.enumerate()

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

Leave a Reply

Your email address will not be published. Required fields are marked *