Go子进程通信

huangapple go评论101阅读模式
英文:

Go subprocess communication

问题

有一种方法可以与等待标准输入的子进程(shell脚本/Python脚本)进行通信。

例如,Python脚本(子进程):

import sys
while True:
    sys.stdout.write('%s\n' % eval(sys.stdin.readline()))

在Go程序中,我想创建这个Python脚本的子进程,并在需要时重复地向其标准输入提供输入,并获取其输出。可以通过向Go程序的标准输出写入或从文件中读取/写入来实现。

以下是我大致尝试的代码,但没有任何反应:

c := exec.Command("python", "-u add.py")
si, _ := c.StdinPipe()
so, _ := c.StdoutPipe()
c.Start()
si.Write([]byte("2+2\n"))

请注意,这只是一个示例,你需要根据你的实际需求进行适当的修改。

英文:

GO: Is there some way to communicate with a subprocess (shell script / python script), which is waiting for input on stdin?

e.g. python script (subprocess)

import sys
while True:
    sys.stdout.write('%s\n'%eval(sys.stdin.readline()))

In the go program, I want to create a subprocess of this python script and provide it input on its stdin, whenever necessary and repeatedly, and take its output. Writing on stdout of Go program or reading/writing from a file will also do.

This is roughly what I am trying, but nothing happens -

c := exec.Command("python", "-u add.py")
si,_ := c.StdinPipe()
so,_ := c.StdoutPipe()    
c.Start()
si.Write([]byte("2+2\n")

答案1

得分: 5

这是您的Go代码的工作版本(Python代码未更改)。

注意:检查所有错误,修复了-u标志,使用bufio读取一行,并使用Wait等待进程结束。

import (
	"bufio"
	"fmt"
	"log"
	"os/exec"
)

func main() {
	c := exec.Command("python", "-u", "add.py")
	si, err := c.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}

	so, err := c.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}
	reader := bufio.NewReader(so)

	err = c.Start()
	if err != nil {
		log.Fatal(err)
	}

	// 现在进行一些数学计算
	for i := 0; i < 10; i++ {
		sum := fmt.Sprintf("2+%d\n", i)
		_, err = si.Write([]byte(sum))
		if err != nil {
			log.Fatal(err)
		}
		answer, err := reader.ReadString('\n')
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("对于 %q 的答案是 %q\n", sum, answer)
	}

	// 关闭输入并等待退出
	si.Close()
	so.Close()
	c.Wait()
}

运行结果为:

对于 "2+0\n" 的答案是 "2\n"
对于 "2+1\n" 的答案是 "3\n"
对于 "2+2\n" 的答案是 "4\n"
对于 "2+3\n" 的答案是 "5\n"
对于 "2+4\n" 的答案是 "6\n"
对于 "2+5\n" 的答案是 "7\n"
对于 "2+6\n" 的答案是 "8\n"
对于 "2+7\n" 的答案是 "9\n"
对于 "2+8\n" 的答案是 "10\n"
对于 "2+9\n" 的答案是 "11\n"
英文:

Here is a working version of your go code (python code is unchanged).

Note: checking of all errors, fixed -u flag, use of bufio to read a line, and Wait to wait for end of process.

import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;os/exec&quot;
)

func main() {
	c := exec.Command(&quot;python&quot;, &quot;-u&quot;, &quot;add.py&quot;)
	si, err := c.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}

	so, err := c.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}
	reader := bufio.NewReader(so)

	err = c.Start()
	if err != nil {
		log.Fatal(err)
	}

	// Now do some maths
	for i := 0; i &lt; 10; i++ {
		sum := fmt.Sprintf(&quot;2+%d\n&quot;, i)
		_, err = si.Write([]byte(sum))
		if err != nil {
			log.Fatal(err)
		}
		answer, err := reader.ReadString(&#39;\n&#39;)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf(&quot;Answer to %q is %q\n&quot;, sum, answer)
	}

	// Close the input and wait for exit
	si.Close()
	so.Close()
	c.Wait()
}

Which produces

Answer to &quot;2+0\n&quot; is &quot;2\n&quot;
Answer to &quot;2+1\n&quot; is &quot;3\n&quot;
Answer to &quot;2+2\n&quot; is &quot;4\n&quot;
Answer to &quot;2+3\n&quot; is &quot;5\n&quot;
Answer to &quot;2+4\n&quot; is &quot;6\n&quot;
Answer to &quot;2+5\n&quot; is &quot;7\n&quot;
Answer to &quot;2+6\n&quot; is &quot;8\n&quot;
Answer to &quot;2+7\n&quot; is &quot;9\n&quot;
Answer to &quot;2+8\n&quot; is &quot;10\n&quot;
Answer to &quot;2+9\n&quot; is &quot;11\n&quot;

huangapple
  • 本文由 发表于 2014年3月27日 14:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/22680080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定