Popen([...], stderr=PIPE) ignores input() message from spawned python program -
i have 1 file running other through popen().
# a.py import subprocess p = subprocess.popen(['python3','/home/scotty/b.py'], stderr=subprocess.pipe) p.wait() # b.py input('???')
when run a.py "???" doesn't appear prompt still works... why? , how can fix it?
if remove stderr=subprocess.pipe "???" show up.
according docs output of input "is written standard output" , i'm not touching standard output.
it seems in exemple, happens if opening pipe stderr not stdout has effect of redirecting stdout stderr because in tests made, stderr receives input's prompt.
below code works me in linux. overcomplicated because use socketpairs, not necessary, @ least robust , works.
################################## # a.py import subprocess import socket import select esock, echildsock = socket.socketpair() osock, ochildsock = socket.socketpair() p = subprocess.popen(['python3','b.py'], stderr=echildsock.fileno(), stdout=ochildsock.fileno()) while p.poll() none: r, w, x = select.select([esock, osock],[],[], 1.0) if not r: continue # timed out s in r: print('stdout ready' if s osock else 'stderr ready') data = s.recv(1024) print('received', data.decode('utf8')) osock.shutdown(socket.shut_rdwr) osock.close() esock.shutdown(socket.shut_rdwr) esock.close() ################################## # b.py res = input('???') print('in b:', res)
wiki
Comments
Post a Comment