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
):
Create a thread that just hangs there for some time:
th = threading.Thread(target=lambda: time.sleep(10))
Start the thread and wait for it:
th.start(); th.join()
While the thread is running, quickly locate the Python process in Task manager
It should have 2 OS threads running (Python actually creates OS threads for each running
threading.Thread
!)Once the thread exits, the Python process will have only one OS thread - the main interpreter thread.
Thus, the thread you created terminated.