python多线程场景下print丢失


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\r' % threading.currentThread().getName())
    logging.info('the arg is:%s\r' %arg)
    print('sub thread start!the thread name is:%s\r' % threading.currentThread().getName())
    print('the arg is:%s\r' %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!')

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM