私信  •  关注

Sud

Sud 最近创建的主题
Sud 最近回复了
7 年前
回复了 Sud 创建的主题 » 强制退出运行python中的线程[duplicate]

使用setdaemon(true)启动子线程。

def bootstrap(_filename):
    mb = ModelBootstrap(filename=_filename) # Has many Daemon threads. All get stopped automatically when main thread is stopped.

t = threading.Thread(target=bootstrap,args=('models.conf',))
t.setDaemon(False)

while True:
    t.start()
    time.sleep(10) # I am just allowing the sub-thread to run for 10 sec. You can listen on an event to stop execution.
    print('Thread stopped')
    break