Use cmd.Start()
and cmd.Wait()
instead of cmd.Run()
.
https://golang.org/pkg/os/exec/#Cmd.Run
Run starts the specified command and waits for it to complete.
Start starts the specified command but does not wait for it to complete.
Wait waits for the command to exit. It must have been started by Start.
And if you want to capture stdout/stderr concurrently, use cmd.StdoutPipe()
/ cmd.StderrPipe()
and read it by bufio.NewScanner()
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
)
func main() {
cmd := exec.Command("python", "game.py")
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
panic(err)
}
go copyOutput(stdout)
go copyOutput(stderr)
cmd.Wait()
}
func copyOutput(r io.Reader) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
The following is a sample python code for reproducing real-time output. The stdout may be buffered in Python. Explicit flush may be required.
import time
import sys
while True:
print "Hello"
sys.stdout.flush()
time.sleep(1)