Running external python in Golang, Catching continuous exec.Command Stdout


So my go script will call an external python like this

cmd = exec.Command("python","")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func(){
    err := cmd.Run()
    if err != nil{

It runs my python script concurrently which is awesome.
But now the problem is, my python script will run infinitely and it will print out some information from time to time. I want to “catch” these Stdout and print them out on my golang terminal. How do I do it concurrently (without waiting my python script to exit)?


Use cmd.Start() and cmd.Wait() instead of 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 (

func main() {
    cmd := exec.Command("python", "")
    stdout, err := cmd.StdoutPipe()
    if err != nil {
    stderr, err := cmd.StderrPipe()
    if err != nil {
    err = cmd.Start()
    if err != nil {

    go copyOutput(stdout)
    go copyOutput(stderr)

func copyOutput(r io.Reader) {
    scanner := bufio.NewScanner(r)
    for scanner.Scan() {

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"

Answered By – minamijoyo

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.