How can I know whether my subprocess is waiting for my input ?(in python3) How can I know whether my subprocess is waiting for my input ?(in python3) python-3.x python-3.x

How can I know whether my subprocess is waiting for my input ?(in python3)


Okay, I've worked it out. My code is based on Non-blocking read on a subprocess.PIPE in python (Thanks, @VaughnCato)

#!/usr/bin/env python3import subprocess as spfrom threading import Threadfrom queue import Queue,Emptyimport timedef getabit(o,q):    for c in iter(lambda:o.read(1),b''):        q.put(c)    o.close()def getdata(q):    r = b''    while True:        try:            c = q.get(False)        except Empty:            break        else:            r += c    return rpobj = sp.Popen('sp.py',stdin=sp.PIPE,stdout=sp.PIPE,shell=True)q = Queue()t = Thread(target=getabit,args=(pobj.stdout,q))t.daemon = Truet.start()while True:    print('Sleep for 1 second...')    time.sleep(1)#to ensure that the data will be processed completely    print('Data received:' + getdata(q).decode())    if not t.isAlive():        break    in_dat = input('Your data to input:')    pobj.stdin.write(bytes(in_dat,'utf-8'))    pobj.stdin.write(b'\n')    pobj.stdin.flush()