连接Golang管道和Python 2.7

时间:2015-07-09 22:25:47

标签: python go

我有以下任务:需要使用Python连接Go管道。我理解如何在Go下运行pythonic进程:

var compiler string = "python"
var path string = "...here script path"
pyExec := exec.Command(compiler, path)
pyExecIn, _ := pyExec.StdinPipe()
pyExecOut, _ := pyExec.StdoutPipe()
pyExec.Start()
// acitions
pyExec.Wait()

我可以在stdin中写入数据,但是只有在终止脚本后才能读取它。 为了获取进程内的数据我决定使用管道,但我只能将消息写入pythonic脚本。 Go尝试从python进程读取数据时被阻止。请回答我,这是实施或Goland管道和pythonic管道不兼容的问题?存在另一种方式连接两个进程(套接字等除外)

去:

var compiler string = "python"
var path string = "...here script path"
pyExec := exec.Command(compiler, path)
pyExecIn, _ := pyExec.StdinPipe()
pyExecOut, _ := pyExec.StdoutPipe()
pipeReader, pipeWriter := io.Pipe()
pyExec.Stdin = pipeReader
pyExec.Stdout = pipeWriter
pyExec.Start()
pipeWriter.Write(message)
var received_data = make([]byte, 1024)
data, _ := pipeReader.Read(received_data)
pyExecIn.Close()
pyExecOut.Close()
pyExec.Wait()

的Python:

r, w = os.pipe()
r = os.fdopen(r, 'r')
raw_responce = r.read(1024)
# actions
w = os.fdopen(w, 'w')
w.write(data)

由于

0 个答案:

没有答案