file sp.py:
#!/usr/bin/env python3
s = input('Waiting for your input:')
print('Data:' + s)
file main.py
import subprocess as sp
pobj = sp.Popen('sp.py',stdin=sp.PIPE,stdout=sp.PIPE,shell=True)
print(pobj.stdout.read().decode())
pobj.stdin.write(b'something...')
print(pobj.stdout.read().decode())
main.py will block in the first pobj.stdout.read()
, because sp.py is waiting for me.
But if I want to process the string 'Waiting for you input:' first, how can I know whether sp.py is waiting for me ?
In other words, I want the pobj.stdout.read()
to return when sp.py is waiting (or sleeping because of time.sleep()
).