subprocess.Popen怎么才能迅速显示命令输出的信息
subprocess.Popen如何才能迅速显示命令输出的信息
ping命令的结果总是需要等待很长时间才能显示出来,如何才能迅速显示呢?
------解决方案--------------------
上述代码稍微修改下,在XP系统下, python 3.2.2 版本下运行正常,代码如下所示:
- Python code
# import subprocess class NetDetect: def __init__(self, addr): self.addr = addr self.log = open( 'NetDetect.log', 'w' ) if self.log == None: print 'Open NetDetect.log failed' def __del__(self): if self.fdp: self.fdp.kill() if self.log: self.log.close() def ping(self): self.fdp = subprocess.Popen( [ 'ping', self.addr, '-t' ], stdout = subprocess.PIPE ) for line in self.fdp.stdout: print line, self.log.write( line ) self.fdp = None if __name__ == '__main__' : nd = NetDetect( 'www.sina.com.cn' ) nd.ping()
ping命令的结果总是需要等待很长时间才能显示出来,如何才能迅速显示呢?
------解决方案--------------------
上述代码稍微修改下,在XP系统下, python 3.2.2 版本下运行正常,代码如下所示:
- Python code
import subprocess class NetDetect: def __init__(self, addr): self.addr = addr self.log = open( 'NetDetect.log', 'w' ) if self.log == None: print('Open NetDetect.log failed') def __del__(self): if self.fdp: self.fdp.kill() if self.log: self.log.close() def ping(self): self.fdp = subprocess.Popen( [ 'ping', self.addr, '-t' ], stdout = subprocess.PIPE ) for line in self.fdp.stdout: print(line.decode("gb2312")) self.log.write(line.decode("gb2312")) self.log.flush() self.fdp = None if __name__ == '__main__' : nd = NetDetect( 'www.sina.com.cn' ) nd.ping()
------解决方案--------------------
嗯,3版的可以跑,还用2版嘛就试试下面dev版本...
http://code.google.com/p/subprocdev/source/browse/subprocess.py?name=default
------解决方案--------------------