Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to a thread executing a method as target

I use some multi-threading in my python application. I want to know what happens to a thread object after it finishes executing the "target" python method. Is it destroyed?

def speak():
    #codes

thread = threading.Thread(target=speak, args=())
thread.start()
like image 534
wmIbb Avatar asked Dec 09 '25 05:12

wmIbb


1 Answers

A simple test shows that the thread is still in memory, "stopped" after it finishes its process:

import threading

def speak():
    pass

thread = threading.Thread(target=speak, args=())
thread.start()
thread.join() # wait for the process to finish;

print thread

# Result: <Thread(Thread-1, stopped 21864)>

From the documentation: "Python’s Thread class supports a subset of the behavior of Java’s Thread class; currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted."

like image 130
Viktor Petrov Avatar answered Dec 10 '25 19:12

Viktor Petrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!