优化前
问题
- 需要全部加载到内存,占用内存
- 且程序结束才看得到,看不到程序进度
- 子进程错误信号没有捕获
优化后
import subprocess
from loguru import logger
if __name__=="__main__":
client_code = "xxx"
cmd_path = "xxx.sh"
run_cmd = f"bash {cmd_path}"
try:
process = subprocess.Popen(
run_cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
except FileNotFoundError:
logger.error(f"{run_cmd} not found")
raise CmdException(client_code, name, cmd, f"{run_cmd} not found")
while True:
output = process.stdout.readline()
if len(output.strip()) == 0 and process.poll() is not None:
break
if output:
logger.info(f"{output.strip()}")
return_code = process.poll()
if return_code != 0:
logger.error(f"{run_cmd} [Error] errcode : {return_code}")
raise CmdException(
client_code, name, cmd, f"{run_cmd} errcode : {return_code}"
)
else:
logger.info(f"{run_cmd} [Success] errcode: {return_code}")
参考
- https://www.bilibili.com/video/BV1oh4y1e73P/?spm_id_from=333.337.search-card.all.click&vd_source=f0cdbfe2b4f49e9bf456390f79118528