如果没有调用方法 thread.stop() 或 thread.join(),python 中的线程什么时候死? [英] When do the threads in python die if the method thread.stop() or thread.join() is not called?

查看:63
本文介绍了如果没有调用方法 thread.stop() 或 thread.join(),python 中的线程什么时候死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一段代码:

def display():
  threading.Timer(1,display).start()
  print("Number")
display()

对于这段代码,我想问以下几点:

For this code I want to ask the following things:

  1. 每隔一秒就会产生一个新线程,对吗?
  2. 每秒钟最后一个线程都会死,因为函数完全执行,所以旧线程会死,对吗?如果不是,那么发生了什么?

推荐答案

  1. Timer 派生自 Thread,所以是的:启动了许多线程.
  2. 线程在被调用的函数返回(或抛出)时死亡,无论您是否调用 join,但是为它们保留的资源可能会或可能不会被回收,直到您这样做.(请注意,线程是常见垃圾收集方案的根源,因此依赖删除对 Thread 对象的引用是不明智的.)
  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.)

这篇关于如果没有调用方法 thread.stop() 或 thread.join(),python 中的线程什么时候死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆