时间提醒
import threading
import logging
import time
def thread_function(name):
logging.info("Thread %s starting", name)
time.sleep(2000)
logging.info("Thread %s finishing", name)
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
logging.info("Main : before creating thread")
x = threading.Thread(target=thread_function, args=(1,))
logging.info("Main : wait for the thread to finish")
logging.info("Main : all done")
daemon
含有daemon标识的线程,当程序结束的时候,线程强制结束
import threading
import logging
import time
def thread_function(name):
logging.info("Thread %s starting", name)
time.sleep(2)
logging.info("Thread %s finishing", name)
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
logging.info("Main : before creating thread")
x = threading.Thread(target=thread_function, args=(1,), daemon=True)
x.start()
logging.info("Main : wait for the thread to finish")
logging.info("Main : all done")
结束子线程后结束主线程
使用join
import threading
import logging