2
我想知道什么时候管道的写入结束关闭。有没有办法做到这一点?找出管道的写入结束是否关闭
from subprocess import Popen, PIPE
p = subprocess.Popen('ls -lh', stdout = PIPE, shell = True)
def read_output(out,queue):
for line in iter(out.readline,b''):
print('.')
t = Thread(target=enqueue_output, args=(p.stdout,q))
t.daemon = True
t.start()
while True:
#this only works if I put
#if line == '' :
# out.close()
#in the for loop of read_output, which I want to avoid.
if p.stdout.closed : #need to find something else than closed here...
break
督察我试图避免必须在IO线程做out.close()......我想以某种方式读出的属性或p.stdout的方法来知道它写目前已关闭。
这实际上不是寻找另一种方式来优雅地阅读Popen的p.stdout,我已经有2种其他方式。这更多是关于学习一些我认为应该可能的事情,但我无法想象如何去做...
干杯!
感谢您的回答,但这不是我要找的。 p是产生shell的子进程。 p.stdout由shell进程编写,并由我读取。我不能也不想模拟shell进程并尝试写入其stdout。我只想知道shell进程是否已关闭管道的末端。如果我在p.stdout而不是readline上执行read_raw会引发EOFError,但是由于readline捕获了这些,所以我什么也得不到。我仍然相信有一种方法可以看到p.stdout的写入侧的句柄已关闭。 – caliloo 2014-12-08 01:41:23