Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot read from Pipe using exec.Command in Go

I am writing a go program that sends data to another program via stdin and reads the response via stdout.

Here is a script which acts as an "echo server" of sorts:

import sys

if __name__=='__main__':
    for line in sys.stdin:
        print("Hello", line.strip())

When I try to communicate with this program in Go, it hangs on buf.ReadLine(). Here is my Go code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package main

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

func main() {
    cmd := exec.Command("python3", "app.py")
    stdout, _ := cmd.StdoutPipe()
    stdin, _ := cmd.StdinPipe()

    cmd.Start()

    stdin.Write([]byte("Bob\n"))

    buf := bufio.NewReader(stdout)
    buf.ReadLine()
    log.Println(buf)
}

The Write() function does not return an error. However, the program hangs when I try to ReadLine(). What am I doing wrong?

>Solution :

It’s not a problem with the Go code; your Python program is buffering output because its stdout is a pipe rather than a terminal, so Go has nothing to read, and you have a deadlock where both processes are waiting for input, and neither one produces any output.

See how can I flush the output of the print function or Disable output buffering for ways to deal with it in Python — the first one works for a single print statement, and the second one for the whole program. For your simple example they’re both the same, but in other cases they may not be so it’s worth understanding the options.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading