无法打印python线程名称

时间:2014-04-16 06:17:52

标签: python python-2.7

我正在尝试打印计时器线程的默认线程名称,但是没有打印,请让我知道我做错了什么。

from threading import current_thread, Timer,Thread
def def1():
    print "inside def1 method",current_thread()

timer1 = Timer(0,def1, [])
timer1.setDaemon(True)
timer1.start()

,输出

inside def1 method

为什么上面的代码没有打印线程名称,请告诉我。我正在使用python 2.7

提前致谢。

1 个答案:

答案 0 :(得分:3)

timer1是一个守护程序线程。最有可能的原因是,在它有机会打印current_thread()的结果之前它会被终止(通过你的进程终止)。

尝试加入线程以查看它是否有所作为(对我而言):

from threading import current_thread, Timer,Thread

def def1():
    print "inside def1 method",current_thread()

timer1 = Timer(0,def1, [])
timer1.setDaemon(True)
timer1.start()
timer1.join()  # <====== ADD THIS