【发布时间】:2015-02-14 23:23:48
【问题描述】:
我的代码如下,基本上这个模块将运行所需的命令并逐行捕获其输出,但在我的情况下,当命令运行时,返回命令提示符只需要一秒钟多的时间,这就是 child .stdout.read(1) 挂起,如果我使用它运行正常命令,它会按预期打印所有内容。但在特定情况下,该命令将某些内容打印到 STDOUT,然后需要一些时间才能返回到提示符,它会挂起.. 请帮助
新代码:
def run_command(shell_command):
'''run the required command and print the log'''
child = subprocess.Popen(shell_command, shell=True,stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = child.communicate()
print stdoutdata
print "Exiting.."
错误:
File "upgrade_cloud.py", line 62, in <module>
stop_cloud()
File "upgrade_cloud.py", line 49, in stop_cloud
run_command(shell_command)
File "upgrade_cloud.py", line 33, in run_command
(stdoutdata, stderrdata) = child.communicate()
File "/usr/lib/python2.6/subprocess.py", line 693, in communicate
stdout = self.stdout.read()
KeyboardInterrupt
【问题讨论】:
-
嗯...我怀疑这可能是issue 7213。您是否有多个 Popen 实例同时运行?
-
不,一次它只运行一个命令就出来了。
-
请帮忙,我被困在这个问题上
-
我唯一能想到的另一件事是实际的命令本身是挂起的。如果是这样的话,Python 就无能为力了。
-
不,不是这样,我可以手动运行命令
标签: python subprocess stdout