为什么这个python脚本会一直等到执行计时器线程?

deo*_*oll 7 python execution

from threading import Timer

def startTimer():

  t = Timer(10.0, foo, ['hello world', 'tell me more'] )
  t.start()
  print 'Timer function invoked'
  print 'function exit'

def foo(msg, msg2):
  print 'foo was executed'
  print msg
  print msg2

if __name__ == '__main__':  
  startTimer()
  print 'end of program'
Run Code Online (Sandbox Code Playgroud)

我已将上述代码保存在文件(timer.py)中,然后在shell中键入python timer.py.但它一直等到foo()被执行.为什么会这样?你怎么称呼这种行为/执行方式?

Joc*_*zel 20

Timer只是一个线程,Python daemonic在停止解释器之前等待所有非线程.

线程可以标记为"守护程序线程".这个标志的意义在于,当只剩下守护进程线程时,整个Python程序都会退出.初始值继承自创建线程.可以通过守护程序属性设置该标志.

来自文档

Set the_timer.daemon=True和Python将立即退出,而不是等待计时器.