Python3 stdout read readline 阻塞情况简单说明
执行命令行,并获取返回结果。
代码1:
process = subprocess.Popen(cmd_string,stdout=subprocess.PIPE,\
universal_newlines=True,\
stderr=subprocess.PIPE,\
shell=False)
while True:
if p.poll() is not None:
break:
outstr = process.read() #此处会阻塞
sleep(1)
简单说明:
# *******obj.read(),obj.readline(),需要读取EOF字符或\n标志结束,会产生阻塞或溢出。*******
# *******使用obj.read(int),不等待结束符,获取足够数量(不超过obj缓存胡数量)即返回。*******
代码2:
process = subprocess.Popen(cmd_string,stdout=subprocess.PIPE,\
universal_newlines=True,\
stderr=subprocess.PIPE,\
shell=False)
while True:
if p.poll() is not None:
break:
outstr = process.read(1) #此处改为read(int)形式,读到数据,就返回结果,不会阻塞
sleep(1)
运行环境,为windows,未在linux中测试。