• 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
    ' % 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!')
  • 相关阅读:
    AUTOSAR-文档阅读
    前端 html
    http协议
    python格式化输出
    IO模型
    协程函数
    伟大的GIL
    苑之歌(进程,线程)
    python之模块导入和包
    任性计算器
  • 原文地址:https://www.cnblogs.com/InformationGod/p/10694032.html
Copyright © 2020-2023  润新知