Home > database >  How to recycle 'threading.Thread' objects in python?
How to recycle 'threading.Thread' objects in python?

Time:09-26

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

CodePudding user response:

"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.

  • Related