python多线程交替打印abc

您所在的位置:网站首页 三个线程交替打印abc python多线程交替打印abc

python多线程交替打印abc

2023-10-08 17:15| 来源: 网络整理| 查看: 265

还是元气满满的小黄鸭呀! 利用python线程的互斥锁可以把资源锁住,然后一个时间只让一个线程执行

关于缓冲区问题:使用 print 输出时会先把数据放到缓冲区里,只有当程序结束时或缓冲区遇到 \n 时,才将数据显示到屏幕,并清空缓冲区,而 print 函数默认会有 \n。

关于换行和缓冲区问题的详细解释:https://blog.csdn.net/yilovexing/article/details/80845514

import threading import sys import time def showa(): while True: lockc.acquire() #获取对方的锁,释放自己的锁 print('a',end='') sys.stdout.flush() #释放缓冲区 locka.release() time.sleep(0.2) def showb(): while True: locka.acquire() print('b',end='') sys.stdout.flush() lockb.release() time.sleep(0.2) def showc(): while True: lockb.acquire() print('c',end='') sys.stdout.flush()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3