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