From aa2d7f3f2d2dbb1c7a87636478556f93827db32b Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 13 Jul 2026 10:03:58 +0800 Subject: [PATCH] fix(runner): use python -u for unbuffered stdout streaming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python defaults to 4KB block buffering when stdout is a pipe (not a TTY). Docker attach uses pipes, so print() output accumulated in the buffer and only flushed when the process exited — the 3-second sleep(1) loop appeared as a single dump at the end. python -u (equivalent to PYTHONUNBUFFERED=1) forces line-level flushing, so each print() is immediately written to the pipe and picked up by the concurrent log reader → SSE → xterm.js. Node/Go/Rust already line-buffer to pipes, so only Python needed the flag. stdout/stderr separation is preserved (no TTY merge). --- src/api/code_runner/languages.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/api/code_runner/languages.rs b/src/api/code_runner/languages.rs index 72d82e0..57abcf4 100644 --- a/src/api/code_runner/languages.rs +++ b/src/api/code_runner/languages.rs @@ -33,7 +33,11 @@ pub static LANGUAGES: LazyLock> = LazyLock::new(|| "python".to_string(), LanguageDef { image: "yggdrasil-runner-python:latest".to_string(), - run_cmd: "python /code/main.py".to_string(), + // -u (unbuffered):强制 stdout/stderr 行刷新。 + // 容器 attach 用 pipe(非 TTY),Python 默认对 pipe 做块缓冲(4KB), + // 导致流式输出失效——print 的内容攒在缓冲区,进程退出才一次性刷出。 + // -u 等价于 PYTHONUNBUFFERED=1,让每行 print 立即写出。 + run_cmd: "python -u /code/main.py".to_string(), extension: "py".to_string(), default_limits: ResourceLimits { cpu_cores: 1.0,