From efcdf6e2d7ecf3ea381459bb9950e1233e7aca74 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 24 May 2026 00:02:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Update=20comments=20and=20docume?= =?UTF-8?q?nt=20OS-specific=20subprocess=20handling=20in=20test=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- subprocess_test_2.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/subprocess_test_2.py b/subprocess_test_2.py index 4ef4c12..a932c53 100755 --- a/subprocess_test_2.py +++ b/subprocess_test_2.py @@ -17,6 +17,7 @@ if __name__ == "__main__": result.check_returncode() print('return code:', result.returncode) print(result.stdout) + print("*" * 100) result = subprocess.run( ['env'], @@ -40,7 +41,7 @@ if __name__ == "__main__": subprocess.run( ['sleep', '5'], env=my_env, - check=True, # 非 0 返回时抛出异常 + check=True, # 非 0 返回时抛出异常,相当于 result.check_returncode() ) print("*" * 100) @@ -48,7 +49,15 @@ if __name__ == "__main__": ['sleep', '50'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, - preexec_fn=os.setpgrp # 让子进程独立,即使当前脚本挂了它也继续运行 + preexec_fn=os.setpgrp # 让子进程独立,即使当前脚本挂了它也继续运行 for Linux/Unix/MacOS ) + # for Windows + # flags = 0x00000008 | 0x00000200 + # process = subprocess.Popen( + # ["cmd", "/c", "your_script.bat"], + # stdout=subprocess.DEVNULL, + # stderr=subprocess.DEVNULL, + # creationflags=flags + # ) print("*" * 100)