python 多线程 print

多线程中使用print函数,有时会出现格式混乱的情况,如下边的代码

import threading
import time

def print1(n):
    for i in range(n):
        print("print1 now is ",n)
        time.sleep(1)
def print2(n):
    for i in range(n):
        print("print2*100 now is ", n*100)
        time.sleep(1)
def main():
    threads = []
    threads.append(threading.Thread(target=print1,args=(10,)))
    threads.append(threading.Thread(target=print2,args=(10,)))
    for t in threads:
        t.start()
    for t in threads:
        t.join()
if __name__ == "__main__":
    main()

可能会产生如下的结果

print1 now is  print2*100 now is 10 
1000
print2*100 now is print1 now is  10
 1000
print2*100 now is  1000
print1 now is  10
print1 now is  10
print2*100 now is  1000
print2*100 now is print1 now is  1000
 10
print1 now is  print2*100 now is  1000
10
print1 now is  10print2*100 now is 
 1000
print2*100 now is  1000
print1 now is  10
print2*100 now is print1 now is  10
 1000
print1 now is print2*100 now is  1000
 10

出现这种情况的原因是有些print的操作不具备原子性,其中print(arg1,arg2,...)不具备原子性,所以会产出上面的问题,修改方法是把所有arg合并到一起,如用print(f"pring1 now is {n}")

修改后的程序如下

import threading
import time

def print1(n):
    for i in range(n):
        print(f"print1 now is {n}")
        time.sleep(1)
def print2(n):
    for i in range(n):
        print(f"print2*100 now is {n*100}")
        time.sleep(1)
def main():
    threads = []
    threads.append(threading.Thread(target=print1,args=(10,)))
    threads.append(threading.Thread(target=print2,args=(10,)))
    for t in threads:
        t.start()
    for t in threads:
        t.join()
if __name__ == "__main__":
    main()

但也会产生问题,如下

print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print2*100 now is 1000print1 now is 10

print2*100 now is 1000print1 now is 10

print1 now is 10print2*100 now is 1000

print1 now is 10
print2*100 now is 1000
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000print1 now is 10

print1 now is 10print2*100 now is 1000

就是两行打印到了一行去,这是由于print的默认end为"\n",但end和print函数不是一起操作的,这就会造成还未来得及打印end就切换到了其他线程去。修改方法可以人为指定换行符,并指定end=‘’ ,如        print(f"print1 now is {n}\n",end='')

修改后的程序如下:

import threading
import time

def print1(n):
    for i in range(n):
        print(f"print1 now is {n}\n",end='')
        time.sleep(1)
def print2(n):
    for i in range(n):
        print(f"print2*100 now is {n*100}\n",end='')
        time.sleep(1)
def main():
    threads = []
    threads.append(threading.Thread(target=print1,args=(10,)))
    threads.append(threading.Thread(target=print2,args=(10,)))
    for t in threads:
        t.start()
    for t in threads:
        t.join()
if __name__ == "__main__":
    main()

可能的结果为

print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print2*100 now is 1000
print1 now is 10
print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print1 now is 10
print2*100 now is 1000
print1 now is 10

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值