Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从后台go进程读取stdin会导致EOF错误_Go_Process_Stdin_Micro - Fatal编程技术网

从后台go进程读取stdin会导致EOF错误

从后台go进程读取stdin会导致EOF错误,go,process,stdin,micro,Go,Process,Stdin,Micro,我正在为它编写一个插件,创建一个后台go进程。当后台进程运行时,它会重复从stdin读取字节,但这始终是一个EOF错误 在Micro中,我的后台进程与JobSpawn函数一样创建,该函数返回一个*exec.cmd: // JobSpawn starts a process with args in the background with the given callbacks // It returns an *exec.Cmd as the job id func JobSpawn(cmdNa

我正在为它编写一个插件,创建一个后台go进程。当后台进程运行时,它会重复从stdin读取字节,但这始终是一个EOF错误

在Micro中,我的后台进程与JobSpawn函数一样创建,该函数返回一个*exec.cmd:

// JobSpawn starts a process with args in the background with the given callbacks
// It returns an *exec.Cmd as the job id
func JobSpawn(cmdName string, cmdArgs []string, onStdout, onStderr, onExit string, userargs ...string) *exec.Cmd {
    // Set up everything correctly if the functions have been provided
    proc := exec.Command(cmdName, cmdArgs...)
    var outbuf bytes.Buffer
    if onStdout != "" {
        proc.Stdout = &CallbackFile{&outbuf, LuaFunctionJob(onStdout), userargs}
    } else {
        proc.Stdout = &outbuf
    }
    if onStderr != "" {
        proc.Stderr = &CallbackFile{&outbuf, LuaFunctionJob(onStderr), userargs}
    } else {
        proc.Stderr = &outbuf
    }

    go func() {
        // Run the process in the background and create the onExit callback
        proc.Run()
        jobFunc := JobFunction{LuaFunctionJob(onExit), string(outbuf.Bytes()), userargs}
        jobs <- jobFunc
    }()

    return proc
}
这是我的流程代码,它使用for循环中的bufio读取器读取stdin:

package main

import ("fmt"
    "bufio"
    "os")

func main() {
    for {
        reader := bufio.NewReader(os.Stdin)
        arr, err := reader.ReadBytes('\n')
        if err != nil {
            fmt.Print(err)
        } else {
            fmt.Print(arr)
        }   
    }
}

生成作业后,它立即开始打印EOF错误。在将任何数据发送到stdin之前,在shell中运行程序时不会发生这种情况。当我给JobSend打电话时,似乎什么也没发生。我甚至添加了一个条件,如果出现错误或数据长度不大于0,则不打印任何内容,但我根本没有收到任何输出。

我认为您需要在运行
之前调用
StdinPipe

package main

import ("fmt"
    "bufio"
    "os")

func main() {
    for {
        reader := bufio.NewReader(os.Stdin)
        arr, err := reader.ReadBytes('\n')
        if err != nil {
            fmt.Print(err)
        } else {
            fmt.Print(arr)
        }   
    }
}