首页 > 解决方案 > 是否可以用 python 解决运行两个可执行文件并且它们可以相互通信(stdin/stdout)?

问题描述

我有两个可执行文件,例如 A.exe、B.exe。python 子进程是否有可能通过stdin/stdout相互通信两个可执行文件:

A = Popen("A.exe",...,stdin=B.stdout, stdout=PIPE)
B = Popen("B.exe",...,stdin=A.stdout, stdout=PIPE) ?

(其中 A.exe 包含 print/scanf 对,B.exe 包含 scanf/printfs。)

标签: pythonredirectstdoutstdin

解决方案


尝试Popen.communicateinput带有文本的参数发送到子进程并返回一个元组(stdout_data, stderr_data)

(output, error) = A.communicate(input="send to a")

推荐阅读