由于某些原因,Popen在调用Powershell脚本后无法退出,并且该命令的输出不是动态的。
我是不是在代码中遗漏了什么:
sCmd = "powershell -file somefile.ps1"
process = subprocess.Popen(sCmd.strip(), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
p = process.communicate()
sys.stdout.write(p[0])
sys.stderr.write(p[1])
sys.stdout.flush()
if(len(p[0]) == 0 and isinstance(process.poll(), int)):
break
if(process.wait() != 0):
os._exit(1)发布于 2012-10-12 04:38:05
验证您的powershell命令是否确实正确退出。挂起的powershell进程很容易导致这种情况。
此外,您也不应该拥有shell=True。这是一个巨大的安全风险。这也可能是为什么这个过程被挂起的原因。如果您的powershell脚本不能运行,除非使用shell=True,那么修复它将是一个很好的解决方案。
https://stackoverflow.com/questions/12847991
复制相似问题