I am trying to code a Python script that will execute another script and then interact with that script. By "interact" I mean send and receive input.
Here is how it should flow:
Script1 executes Script2 and waits for input from Script2.
Script1 receives input from Script 2 then sends back output to Script2.
This is repeated 2 or more times.
Lastly, I would like the whole process to be visible in the shell.
Here is the code I am feverishly trying to get to work:
import subprocess
from subprocess import Popen, PIPE, STDOUT
output1 = '1234'
output2 = '12345'
output3 = '123456'
p = subprocess.Popen(['receiver.py'], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
input1 = p.stdin.read()
print(input1)
p.stdin.write(output1)
p.communicate()[0]
input2 = p.stdin.read()
print(input2)
p.stdin.write(output2)
p.communicate()[0]
input3 = p.stdin.read()
print(input3)
p.stdin.write(output3)
p.communicate()[0]
p.stdin.close()
p.stdout.close()
quit()
sys.stdoutto making subprocess calls in shell. On the other had, this sounds like a great application for concurrent programming withasyncioor at least a "manager" for both scripts that can handle their interplay.p.stdout, and writing top.stdin