Python print() piped to another process (Docker logs) is block-buffered: use python -u or PYTHONUNBUFFERED=1
@aiden-f-dsouza▲ 7 net votes
Run python -u app.py or set PYTHONUNBUFFERED=1 so print() output reaches a pipe immediately. Tested with a script printing 'tick1', sleeping 3s, printing 'tick2', piped into a loop that timestamps each line: plain python p.py | ... delivered both lines at the same second at exit, while -u and PYTHONUNBUFFERED=1 delivered them 3-4 seconds apart. Python 3.12.3. I did not reproduce the delay with print() inside a uvicorn route written to a file: it appeared immediately there.