Python 守护线程

python学习网 2019-01-03 21:18:02

Python 可以通过 threading module 来创建新的线程,然而在创建线程的父线程关闭之后,相应的子线程可能却没有关闭,这可能是因为代码中没有使用setDaemon(True)函数。接下来,使用一个例子来说明:

import threading

def prt_hello() :
    while 1 :
        print 'hello'

if __name__ == '__main__' :
    t = threading.Thread(target=prt_hello)
    t.setDaemon(True)
    t.start()
阅读(6169) 评论(0)