コスギデンサン >> 情報系メモ >> Python

Python - デーモンスレッド 2018/9
Version 3.6.1 ON CentOS7

メインスレッドが終了したのにサブスレッドが終わらない。
import threading
import time
import sys

def child():
    while(True):
        time.sleep(1)
            print(".")


t1 = threading.Thread(target=child)
t1.start()

time.sleep(10)
print("end")

デーモンスレッドにすると、メインスレッドが終了したらサブスレッドも終わる。
import threading
import time
import sys

def child():
    while(True):
        time.sleep(1)
            print(".")


t1 = threading.Thread(target=child)
t1.setDaemon(True)
t1.start()

time.sleep(10)
print("end")