python多线程情况下,print输出会出现丢失的情况,而logging模块的日志输出不会。
以下是示例代码,多运行几次就会发现这个有意思的现象
# coding:utf-8 import threading import time import logging def action(arg): time.sleep(1) logging.info('sub thread start!the thread name is:%s ' % threading.currentThread().getName()) logging.info('the arg is:%s ' %arg) print('sub thread start!the thread name is:%s ' % threading.currentThread().getName()) print('the arg is:%s ' %arg) time.sleep(1) if __name__=="__main__": logging.basicConfig(level=logging.INFO) for i in range(4): t =threading.Thread(target=action,args=(i,)) t.setDaemon(False)#设置线程为后台线程 t.start() print('main_thread end!')