Tuesday 26 September 2023

find out if the write end of a pipe is closed

I'm trying to figure a way to know when the write end of a pipe gets closed. Is there a way to do that somehow ?

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

iow I'm trying to avoid to have to do out.close() in the io Thread.... I want to somehow read a property or a method of p.stdout to know if its write end has been closed.

This is really not about finding another way to read Popen's p.stdout gracefully, I have 2 others ways already. It's more about learning something I thought should be possible, but I havn't figured how to do...

Cheers !



from find out if the write end of a pipe is closed

No comments:

Post a Comment