mirror of
https://github.com/element-hq/synapse.git
synced 2025-12-05 01:10:13 +00:00
Explain why we flush_buffer() for Python print(...) output (#18420)
Spawning from using this code elsewhere and not knowing why it's there. Based on this article and @reivilibre's experience mentioning `PYTHONUNBUFFERED=1`, > #### programming languages where the default “print” statement buffers > > Also, here are a few programming language where the default print statement will buffer output when writing to a pipe, and some ways to disable buffering if you want: > > - Python (disable with `python -u`, or `PYTHONUNBUFFERED=1`, or `sys.stdout.reconfigure(line_buffering=False)`, or `print(x, flush=True)`) > > _-- https://jvns.ca/blog/2024/11/29/why-pipes-get-stuck-buffering/#programming-languages-where-the-default-print-statement-buffers_
This commit is contained in:
1
changelog.d/18420.misc
Normal file
1
changelog.d/18420.misc
Normal file
@@ -0,0 +1 @@
|
||||
Explain why we `flush_buffer()` for Python `print(...)` output.
|
||||
@@ -352,6 +352,11 @@ def error(txt: str) -> NoReturn:
|
||||
|
||||
|
||||
def flush_buffers() -> None:
|
||||
"""
|
||||
Python's `print()` buffers output by default, typically waiting until ~8KB
|
||||
accumulates. This method can be used to flush the buffers so we can see the output
|
||||
of any print statements so far.
|
||||
"""
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
@@ -22,6 +22,11 @@ def error(txt: str) -> NoReturn:
|
||||
|
||||
|
||||
def flush_buffers() -> None:
|
||||
"""
|
||||
Python's `print()` buffers output by default, typically waiting until ~8KB
|
||||
accumulates. This method can be used to flush the buffers so we can see the output
|
||||
of any print statements so far.
|
||||
"""
|
||||
sys.stdout.flush()
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user