Python Windows下subprocess立即关闭管道

Windows下带UI的Python程序,如果其中使用了subprocess.Popen,那么我们可以设置shell=True来不显示command窗口,但是这样的话,使用terminate()就无法立即终止管道;如果想terminate()能达到立即终止的效果,又必须把shell=False,看起来是二选一。没关系,可以用creationflags来达到两全:

import subprocess

pipe = subprocess.Popen('ping 127.0.0.1', stdout=subprocess.PIPE, creationflags=0x08)


由于shell=False是默认值,所以未设置。这样一来,执行时即不显示command窗口,又可以用terminate()来立即终止管道。

猜你喜欢

转载自blog.csdn.net/wn0112/article/details/9113361