擅长:python、mysql、java
<p>这个怎么样:</p>
<pre><code>from subprocess import Popen, PIPE
def log_command_outputs(commands):
processes = [Popen(cmd, stdout=PIPE) for cmd in commands]
outputs = [proc.communicate()[0].split() for proc in processes]
for output in outputs:
for line in output:
script_log.write(line)
script_long.write("\n")
</code></pre>
<p>这会并行地启动命令,这可能会比一个接一个地执行命令快一点(但可能不会太快)。但是,由于<code>communicate</code>调用是连续的,因此任何具有大输出(超过管道缓冲区)的命令都将阻塞,直到轮到它被清除为止。</p>
<p>对于您的示例命令链,您可以调用:</p>
<pre><code>log_command_outputs([["mmls", "WinXP.E01"], ["stat", "WinXP.E01"]])
</code></pre>