📝 Update README.md with Python print buffering documentation and solutions

This commit is contained in:
2026-05-24 07:12:41 +08:00
parent 3613252f1e
commit 0c497641ca
2 changed files with 32 additions and 0 deletions
+20
View File
@@ -79,3 +79,23 @@ if __name__ == "__main__":
```shell
./show_myip.py
```
<br>
在 Python 中,print() 函数默认会对输出进行缓存(buffering)。
```python
import time
for i in range(5, 0, -1):
# end="" 让它不换行,flush=True 强制立即刷新缓存
print(f"\rcountdown: {i}", end="", flush=True)
time.sleep(1)
print("\ndone")
```
通过设置环境变量也可以禁止缓存:
```shell
export PYTHONUNBUFFERED=1
```