0

I've been trying to use a python utility from code that I'm writing in go. I've been trying to use stdin/stdout to communicate between the processes. However, I'm getting an EOF error using python's raw_input(), even if I connect its stdin to go's stdin.

Here is the code to reproduce the issue:

test.go:

package main

import (
    "os"
    "os/exec"
)

func main() {
    cmd := exec.Command("python", "test.py")

    cmd.Stderr = os.Stderr
    cmd.Stdout = os.Stdout
    cmd.Stdin = os.Stdin

    // Start the process
    if err := cmd.Start(); err != nil {
        panic(err)
    }
}

test.py:

while True:
    input = raw_input()
    print input

The error I get is

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    input = raw_input()
EOFError

I don't understand why this would be an issue. Does anybody have any input?

1 Answer 1

2

When cmd.Start() returns, the process running test.go exits and its standard input is closed; so, the process running test.py receives EOF.

Modify test.go, adding something like cmd.Wait(), or even just a time.Sleep(300 * time.Second) after the last block, and you'll see what you probably expected.

For instance:

package main

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

func main() {
        cmd := exec.Command("python", "test.py")

        cmd.Stderr = os.Stderr
        cmd.Stdout = os.Stdout
        cmd.Stdin = os.Stdin

        // Start the process
        if err := cmd.Start(); err != nil {
                panic(err)
        }
        if err := cmd.Wait(); err != nil { 
                fmt.Println("cmd.Wait() in parent process:", err)
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see. That does make sense. So, what if I want the python script to run indefinitely? Does it make sense for me to just start a new goroutine, start the python process there, then do cmd.Wait() from that routine? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.