fix: 部署脚本子进程输出被Python缓冲的问题
子进程Python脚本运行时,Python自动缓冲stdout(因为检测到非TTY)。 设置PYTHONUNBUFFERED=1环境变量,确保所有Python子进程实时输出。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -82,17 +82,20 @@ def log_section(msg):
|
||||
|
||||
def run_command(cmd, cwd=None, timeout=120, capture=True):
|
||||
"""执行本地命令"""
|
||||
# 设置 PYTHONUNBUFFERED=1 确保 Python 子进程不缓冲 stdout
|
||||
env = os.environ.copy()
|
||||
env['PYTHONUNBUFFERED'] = '1'
|
||||
try:
|
||||
if capture:
|
||||
result = subprocess.run(
|
||||
cmd, shell=True, cwd=cwd,
|
||||
cmd, shell=True, cwd=cwd, env=env,
|
||||
capture_output=True, text=True, encoding='utf-8', errors='replace',
|
||||
timeout=timeout
|
||||
)
|
||||
return result.returncode == 0, result.stdout.strip(), result.stderr.strip()
|
||||
else:
|
||||
result = subprocess.run(
|
||||
cmd, shell=True, cwd=cwd, timeout=timeout
|
||||
cmd, shell=True, cwd=cwd, env=env, timeout=timeout
|
||||
)
|
||||
return result.returncode == 0, "", ""
|
||||
except subprocess.TimeoutExpired:
|
||||
|
||||
Reference in New Issue
Block a user