问题
Here is a snippet of code:
def display():
threading.Timer(1,display).start()
print("Number")
display()
For this code I want to ask the following things:
- Every second new thread spawns, is that right?
- Every second the last thread dies because the function executes completely so the older thread dies, is that right? If not then what is happening?
回答1:
Timerderives fromThread, so yes: many threads are started.- Threads die when their invoked functions return (or throw) whether or not you call
join, but resources reserved for them may or may not be reclaimed until you do. (Note that threads are roots in common garbage-collection schemes, so it’s unwise to rely on dropping references toThreadobjects.)
来源:https://stackoverflow.com/questions/53916471/when-do-the-threads-in-python-die-if-the-method-thread-stop-or-thread-join-i