0

I was wondering how you would recycle 'threading.Thread' objects in python?

Firstly, I create (x) amount of threads("workers") and store within a list. I start them as usual and join them once they've finished. Then I file data from data gathered from a shared object. Finally, I clear all threads("workers") within the list and repeat this cycle within a conditional loop.

The problem I seem to be having is that the thread objects still exist even after removing them. Say I use 10 threads, the next time it cycles through I end up using thread 11-20.

Is there a way to terminate the the first cycle of threads after joining and effectively recycle the same threads?

Kind regards,

Andrew

1
  • Thread IDs are not dense sequences: what leads to the assertion of “using thread 11-20” and “[thread objects] still exist”? (A thread is finished — and is_alive is reset to False —when it’s run method terminates. Without a timeout, a join is guaranteed to wait for this condition and the thread is “no longer alive” and the actual Python thread wrapper object follows normal lifetime (and GC) rules.) Commented Sep 25, 2021 at 20:55

1 Answer 1

1

"Is there a way to terminate the the first cycle of threads after joining" - joining is the same as terminating, though. the_thread.name looks like 'Thread-N', where N is the "number" of a thread, which is normally increased for each new thread. This doesn't necessarily mean that if this thread is 'Thread-100', then the previous 99 threads are still running.

Joining a thread means terminating it, so no stray threads will be left running.

Example

>>> import threading, time
>>> th = threading.Thread(target=lambda: time.sleep(10))
>>> th.start(); th.join()
>>> th.is_alive()
False # the thread is dead
>>> th.name
'Thread-1'
>>> th2 = threading.Thread(target=lambda: time.sleep(10))
>>> th2.start(); th2.join()
>>> th2.is_alive(), th2.name
(False, 'Thread-2') # This thread is dead alongside Thread-1
>>> 

You can test it by looking at how many threads your Python process has (in Task manager or maybe something like top):

  1. Create a thread that just hangs there for some time:

    th = threading.Thread(target=lambda: time.sleep(10))
    
  2. Start the thread and wait for it: th.start(); th.join()

  3. While the thread is running, quickly locate the Python process in Task manager

  4. It should have 2 OS threads running (Python actually creates OS threads for each running threading.Thread!)

  5. Once the thread exits, the Python process will have only one OS thread - the main interpreter thread.

  6. Thus, the thread you created terminated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.