擅长:python、mysql、java
<p>不知道你的代码出了什么问题,但下面的代码似乎对我有用:</p>
<pre><code>#!/usr/bin/python
from subprocess import Popen, PIPE
import threading
p = Popen('ls', stdout=PIPE)
class ReaderThread(threading.Thread):
def __init__(self, stream):
threading.Thread.__init__(self)
self.stream = stream
def run(self):
while True:
line = self.stream.readline()
if len(line) == 0:
break
print line,
reader = ReaderThread(p.stdout)
reader.start()
# Wait until subprocess is done
p.wait()
# Wait until we've processed all output
reader.join()
print "Done!"
</code></pre>
<p>注意,我没有安装Ruby,因此无法检查您的实际问题。不过,在使用<code>ls</code>时效果很好。</p>