[英]threading.Timer() and destruction of instance
在交互式python中,我做了以下實驗:
import threading
def hello():
print "Hello World!"
def greeting():
t = threading.Timer(5, hello)
t.start()
greeting()
"Hello World!" # After 5s delay.
令我驚訝的是延誤后才打印出問候語。 我以為離開范圍會破壞Timer實例,但是python的垃圾收集器將扮演那個角色是有意義的嗎?
對於我的需求,上述實驗中的行為是我所需要的,但是我認為它不能保證始終有效。 還是會一直按所示運行?
謝謝。
可以肯定地認為計時器可以存活。
Timer
是從Thread
派生的,並且正在運行的線程不會在Python中被殺死,因為不再存在對該線程對象的引用(當然,始終存在Python解釋器本身保留的引用)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.