10.1.2.2 与进程的双向通信
要建立Popen实例同时完成读写,可以结合使用前面几个技术。
import subprocess
print('popen2:')
proc = subprocess.Popen(
['cat','-'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
msg = 'through stdin to stdout'.encode('utf-8')
stdout_value = proc.communicate(msg)[0].decode('utf-8')
print('pass through:',repr(stdout_value))
这会建立管道来模拟popen2()。
运行结果: