问答 请教:stdout.readline ().strip () 在 MAC 环境上一直处于阻塞,代码运行到这个地方就不动了

erinet · 2017年05月16日 · 最后由 erinet 回复于 2017年05月17日 · 1857 次阅读

‘’‘
def cmd(cmd):
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
’‘’
‘’‘
appium = cmd("appium -p %s -bp %s -U %s" % (aport, bpport, self.device)) # 启动 appium
while True:
appium_line = appium.stdout.readline().strip()
if 'listener started' in appium_line:
break
’‘’

以上代码在 MAC 上运行,运行到 appium_line = appium.stdout.readline().strip() 代码就一定阻塞在这里,麻烦有神解答下吗,谢谢

共收到 3 条回复 时间 点赞

有耐心的话,可以参考我的这个实现:
https://github.com/264768502/adb_wrapper/blob/master/adb_wrapper/base_wrapper.py
主要看_command_blocking 这个函数
兼容了 py2 和 py3

具体原理可以参考一下 sf 的回答
http://stackoverflow.com/questions/2715847/python-read-streaming-input-from-subprocess-communicate/17698359#17698359

LZ 贴的这代码好像是我写的。。。当时实现的时候没想缓冲区和子进程的问题。我看@264768502写的了,很好,学习了,已 star

joko 回复

是的 谢谢两位大神

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册