When do the threads in python die if the method thread.stop() or thread.join() is not called?

十年热恋 提交于 2021-02-18 18:03:08

问题


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:

  1. Every second new thread spawns, is that right?
  2. 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:


  1. Timer derives from Thread, so yes: many threads are started.
  2. 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 to Thread objects.)


来源:https://stackoverflow.com/questions/53916471/when-do-the-threads-in-python-die-if-the-method-thread-stop-or-thread-join-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!