0

Python 3.7

test.py

proc = subprocess.Popen(['python3.7', 'mirror.py'],
                        bufsize=0,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

proc.stdin.write('1234\n'.encode())
proc.stdin.flush()
print(proc.stdout.read())

mirror.py

np = input()
print(np[::-1])

So I want to run mirror.py continuously and interract (send message to and reach ansver from) them more than one time. But when I try to add While True loop to mirror.py I got stuck: test.py not showing anything I try to change input and print to sys.stdin/out.read/write and got the same I try to change loop to multiply input/print pairs and got the same

I think I need to stop reading from stdout (maybe send something special from mirror.py after mirrored input) Can any one help me to reach the goal?

Thx

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Why are you running Python as a subprocess of Python? You get a lot better control by using multiprocessing or threading and keeping it in a single program. – tripleee Feb 06 '20 at 10:18
  • You need loops in both test.py and mirror.py. – martineau Feb 06 '20 at 10:33
  • @tripleee >Why are you running Python as a subprocess of Python? This is a concept of wrapper, that would start different engins and propogate tasks to it. – Oleg Borisov Feb 06 '20 at 10:59
  • @martineau >You need loops in both test.py and mirror.py. start subproc in loop is not an option – Oleg Borisov Feb 06 '20 at 11:03
  • You can use multiprocessing here. Please refer the below link. https://docs.python.org/2/library/multiprocessing.html – Anupam Chaplot Feb 06 '20 at 11:52
  • IDK how to use formatting and can't insert solution. But I fix it. import sys i = 0 while True: i += 1 sys.stdin.flush() a = input() sys.stdout.write(f'{a[::-1], i}\n') – Oleg Borisov Feb 06 '20 at 12:07

0 Answers0