擅长:python、mysql、java
<p>与其使用<code>osstdout.stdout.read()</code>来获取子进程的<code>stdout</code>,不如使用<a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">^{<cd3>}</a>来获取子进程的<code>stdout</code>,这将阻塞直到子进程终止。完成后,属性<code>osstout.returncode</code>将被设置为包含子流程的返回代码。</p>
<p>你的函数可以写成</p>
<pre><code>def OSinfo(runthis):
osstdout = subprocess.Popen(runthis, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
theInfo = osstdout.communicate()[0].strip()
return (theInfo, osstout.returncode)
</code></pre>