Python线程中打印为何呈队列式延迟输出?如何实现实时打印?
问题描述
我正在编写一个需要同时处理用户输入与打印操作的Python程序,使用了threading模块,但发现打印无法实时输出。以下是复现问题的POC代码:
└─# python3 Python 3.11.4 (main, Jun 7 2023, 10:13:09) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import threading >>> import time >>> >>> def print_something(): ... while 1: ... print("This is an important string", end="") ... time.sleep(1) ... >>> >>> thread = threading.Thread(target=print_something) >>> thread.daemon = True >>> thread.start() This is an important string>>> This is an important stringThis is an important string>>> This is an important string>>> >>> >>> input() This is an important stringThis is an important stringThis is an important stringThis is an important string'' >>> This is an important stringThis is an important string>>> >>>
如上述示例所示,打印的"This is an important string"仅在按下回车键时才会批量输出,而非每间隔1秒实时打印。请问该现象的原因是什么?如何实现无需按回车即可实时打印的效果?
原因分析
这是因为Python的标准输出(stdout)默认采用行缓冲模式:只有当输出内容包含换行符\n、缓冲区被手动刷新,或者程序终止时,缓冲区里的内容才会真正输出到终端。你的代码中使用print(..., end="")取消了默认的换行符,导致输出内容一直积压在缓冲区中,直到你按下回车触发input()操作时,终端才会强制刷新缓冲区,把积压的内容一次性输出。
另外,在交互式Python环境中,输入操作会抢占终端焦点,同时触发缓冲区刷新,这也是为什么只有按回车后才会看到批量输出的核心原因。
解决方案
手动刷新标准输出缓冲区
在每次print调用后,使用sys.stdout.flush()强制刷新缓冲区,让内容立刻输出到终端:import threading import time import sys def print_something(): while True: print("This is an important string", end="") sys.stdout.flush() # 强制刷新缓冲区 time.sleep(1) thread = threading.Thread(target=print_something) thread.daemon = True thread.start() input()利用
print函数的flush参数
Python 3.3及以上版本的print函数提供了flush参数,将其设置为True可以自动在打印后刷新缓冲区,无需手动调用flush():import threading import time def print_something(): while True: print("This is an important string", end="", flush=True) # 自动刷新缓冲区 time.sleep(1) thread = threading.Thread(target=print_something) thread.daemon = True thread.start() input()
这两种方法都能让打印内容实时显示在终端,不需要等待回车键触发缓冲区刷新。
内容的提问来源于stack exchange,提问作者Onyz
相关产品推荐
相关产品推荐

