I am trying to run a subprocess with Popen, and at a time it asks for an input prompt without any EOF, so stdout.read() blocks the while loop until an EOF is found, like forever because there will be none.
I am unable to detect if we are in an input prompt coming next via
proc.stdout.isatty()it stays at Falseproc.stdout.writable()it stays at False
main1.py
from subprocess import Popen, PIPE
import sys
proc = Popen(["python3", "main2.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
def read_until_it_finishes():
while proc.poll() is None:
if proc.stdout.isatty() is True: # <-- Why this line isn't it detecting we are in a input prompt ?
break
if proc.stdout.writable() is True: # <-- Why this line isn't it detecting we are in a input prompt ?
break
line = proc.stdout.read(1).decode(sys.stdout.encoding) # https://stackoverflow.com/a/63137801/10294022
sys.stdout.write(line)
sys.stdout.flush()
read_until_it_finishes()
proc.stdin.write(b"My name is Catwoman\n")
proc.stdin.flush()
main2.py
import sys
sys.stdout.write("Hello my name is Batman\n")
sys.stdout.flush()
sys.stdout.write("I am awesome\n")
sys.stdout.flush()
name = input('And you what is your name:')
sys.stdout.write(name)
sys.stdout.flush()
sys.stdout.close()
Then run
python3 main1.py
Would you please help me ?
proc.stdin.write(b"My name is Catwoman\n")→proc.stdin.flush()→read_until_it_finishes()think? Here is nothinktime even betweenread_until_it_finishes()andproc.stdin.write(b"My name is Catwoman\n"). It will be run as solid-single piece of code