GO: 有没有一种方式可以与等待stdin输入的子进程(shell脚本/Python脚本)进行通信?
例如:Python脚本(子进程)
import sys
while True:
sys.stdout.write('%s\n'%eval(sys.stdin.readline()))
在go程序中,我想创建这个python脚本的子进程,并在必要时反复提供其stdin输入,并获取其输出。可以写入Go程序的stdout或从文件读取/写入也可以。
这大致是我正在尝试的,但什么也没有发生-
c := exec.Command("python", "-u add.py")
si,_ := c.StdinPipe()
so,_ := c.StdoutPipe()
c.Start()
si.Write([]byte("2+2\n")
eval(sys.stdin.readline())
替换为input()
。或者使用import code; shell = code.InteractiveConsole(); shell.interact()
替换整个循环。或者只需启动python -i
进程即可。 - jfsexec.Command("python", "-u", "add.py")
或者exec.Command("python", "-u", "-i")
或者exec.Command("python", "-i")
(如果它不能使输出完全缓冲)。 - jfs