douyueqing1530 2015-07-09 22:25
浏览 55

连接Golang管道和Python 2.7

I have following task: need connecting Go pipe with Python. I understand how run pythonic process under Go:

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()

I can write data in stdin but I read it only after terminating script. For get data inside process I decide use pipe, but I only can write message into pythonic script. When Go trying read data from python process is blocked. Answer me please, this is trouble with implementation or Goland pipes and pythonic pipes not compatible? Exist another way connection both process (except socket and so on)

Go:

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)

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮