Python 2.7 Python2.7中的子进程发送输入

Python 2.7 Python2.7中的子进程发送输入,python-2.7,subprocess,Python 2.7,Subprocess,我已经成功地从一个单独的python脚本(在Python3.4中称为from popen subprocess)通过管道传输了输出和输入,将原始输入更改为输入,但它必须在2.7环境中工作。在2.7中工作时,当前不显示输出。我怎样才能在2.7小时内完成这项拖车工作 test.py: import threading import subprocess def read_output(process): print("starting to read") for line in p

我已经成功地从一个单独的python脚本(在Python3.4中称为from popen subprocess)通过管道传输了输出和输入,将原始输入更改为输入,但它必须在2.7环境中工作。在2.7中工作时,当前不显示输出。我怎样才能在2.7小时内完成这项拖车工作

test.py:

import threading
import subprocess

def read_output(process):
    print("starting to read")
    for line in process.stdout:
       print (line.rstrip().decode('utf-8'))


process = subprocess.Popen('python test2.py', shell=False,
                           stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                           stderr=None)

# Create new thread: pass target and argument
thread1 = threading.Thread(target=read_output,args=(process,))

# Start new Threads
thread1.daemon=True
thread1.start()

s=input("")

process.stdin.write(s.encode('utf-8'))
process.stdin.write("\r\n".encode('utf-8'))
process.stdin.flush()  # close standard input or thread doesn't terminate

thread1.join()
简单测试脚本test2.py:

print("running script 2")
print("Enter an input A,B,C:")

s=raw_input("")
print("you selected:"+s)

你必须在进程中写入一些东西。然后刷新它。我在两个位置这样做,你说它应该出现在哪里?我读错了。应该可以正常工作(我不明白为什么发送两个字符串,而测试程序只接受一个并退出)。也许您必须在
test2.py
脚本中使用
sys.stdout.flush()
。最终目标是不控制测试脚本—修改它是不可行的。