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!')

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2021-06-29
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-11
  • 2021-08-16
  • 2022-12-23
  • 2021-12-04
  • 2021-12-22
  • 2022-12-23
  • 2021-09-06
相关资源
相似解决方案