在Golang中运行外部python,捕捉连续的exec.Command Stdout [英] Running external python in Golang, Catching continuous exec.Command Stdout

查看:579
本文介绍了在Golang中运行外部python,捕捉连续的exec.Command Stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的脚本会像这样调用一个外部python
$ b $ $ p code> cmd = exec.Command(python,game .py)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr $ b $ go func(){
err:= cmd.Run()
if err!= nil {
panic(err)
}
}()

它同时运行我的python脚本,非常棒。
但现在的问题是,我的python脚本会无限运行,它会不时打印出一些信息。我想要抓住这些Stdout并在我的golang终端上打印出来。如何同时执行(不等待我的python脚本退出)?

Start()和 cmd.Wait()而不是 cmd.Run()



https:// golang.org/pkg/os/exec/#Cmd.Run


运行开始指定的命令并等待它完成。

开始启动指定的命令,但不等待它完成。



等待等待命令退出。它必须由Start启动。


如果要同时捕获stdout / stderr,请使用 cmd .StdoutPipe() / cmd.StderrPipe()并读取 bufio.NewScanner()















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

以下是用于复制实时输出的示例python代码。标准输出可以用Python进行缓冲。

 进口时间
进口sys

,而True:
printHello
sys.stdout.flush()
time.sleep(1)


So my go script will call an external python like this

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

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

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)

这篇关于在Golang中运行外部python,捕捉连续的exec.Command Stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆