Go:与另一个进程的双向通信?

Go:与另一个进程的双向通信?,go,popen,Go,Popen,注意,没有一个重复询问System V IPC。尾注 使用os/exec,如何与另一个进程交互通信?我希望为进程的stdin和stdout获取fd,并使用这些fd对进程进行写入和读取 我发现的大多数示例都涉及到运行另一个进程,然后输出结果 这是我正在寻找的python等价物 p = subprocess.Popen("cmd", stdin=subprocess.PIPE, stdout=subprocess.PIPE) (child_stdin, child_stdout) = (p.stdi

注意,没有一个重复询问System V IPC。尾注

使用os/exec,如何与另一个进程交互通信?我希望为进程的stdin和stdout获取fd,并使用这些fd对进程进行写入和读取

我发现的大多数示例都涉及到运行另一个进程,然后输出结果

这是我正在寻找的python等价物

p = subprocess.Popen("cmd", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)

作为一个具体的例子,考虑打开一个管道到DC,发送线路12 34 +P并接收线路46。 更新

我清楚地看到dc正在按预期接收和应答:

[pid  8089] write(4, "12 23 +p\n", 9 <unfinished ...>
...
[pid  8095] <... read resumed> "12 23 +p\n", 4096) = 9
...
[pid  8095] write(1, "35\n", 3 <unfinished ...>
更新

根据接受的答案,我的问题不是分配字符串来接收响应。更改为第行:=make[]byte,100修复了所有问题。

exec.Cmd包含可分配的进程stdin、std和stderr字段

    // Stdin specifies the process's standard input.
    // If Stdin is nil, the process reads from the null device (os.DevNull).
    // If Stdin is an *os.File, the process's standard input is connected
    // directly to that file.
    // Otherwise, during the execution of the command a separate
    // goroutine reads from Stdin and delivers that data to the command
    // over a pipe. In this case, Wait does not complete until the goroutine
    // stops copying, either because it has reached the end of Stdin
    // (EOF or a read error) or because writing to the pipe returned an error.
    Stdin io.Reader

    // Stdout and Stderr specify the process's standard output and error.
    //
    // If either is nil, Run connects the corresponding file descriptor
    // to the null device (os.DevNull).
    //
    // If Stdout and Stderr are the same writer, at most one
    // goroutine at a time will call Write.
    Stdout io.Writer
    Stderr io.Writer
如果要将预制管道连接到其中任何一个管道,可以使用*pipe方法

使用dc程序sans错误检查的基本示例:

cmd := exec.Command("dc")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
cmd.Start()

stdin.Write([]byte("12 34 +p\n"))

out := make([]byte, 1024)
n, _ := stdout.Read(out)

fmt.Println("OUTPUT:", string(out[:n]))

// prints "OUTPUT: 46"
Cmd具有进程stdin、std和stderr的字段,您可以分配这些字段

    // Stdin specifies the process's standard input.
    // If Stdin is nil, the process reads from the null device (os.DevNull).
    // If Stdin is an *os.File, the process's standard input is connected
    // directly to that file.
    // Otherwise, during the execution of the command a separate
    // goroutine reads from Stdin and delivers that data to the command
    // over a pipe. In this case, Wait does not complete until the goroutine
    // stops copying, either because it has reached the end of Stdin
    // (EOF or a read error) or because writing to the pipe returned an error.
    Stdin io.Reader

    // Stdout and Stderr specify the process's standard output and error.
    //
    // If either is nil, Run connects the corresponding file descriptor
    // to the null device (os.DevNull).
    //
    // If Stdout and Stderr are the same writer, at most one
    // goroutine at a time will call Write.
    Stdout io.Writer
    Stderr io.Writer
如果要将预制管道连接到其中任何一个管道,可以使用*pipe方法

使用dc程序sans错误检查的基本示例:

cmd := exec.Command("dc")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
cmd.Start()

stdin.Write([]byte("12 34 +p\n"))

out := make([]byte, 1024)
n, _ := stdout.Read(out)

fmt.Println("OUTPUT:", string(out[:n]))

// prints "OUTPUT: 46"

@MarioAlexandroSantini,不是复制品。我不是在找system-V IPC,比如shmget。您正在尝试读取长度为0的片,因此无法读取任何数据。在我的例子中,需要有一些东西可以读懂,看出来。这非常有效。谢谢你的洞察力@布里安托姆塞特-汤莱恩, 这和MarioAlexandroSantin提出并删除的建议是一样的?。这不是一个重复,因为这个问题似乎围绕着system-V IPC。注意到并收回。谢谢。@MarioAlexandroSantini,不是复制品。我不是在找system-V IPC,比如shmget。您正在尝试读取长度为0的片,因此无法读取任何数据。在我的例子中,需要有一些东西可以读懂,看出来。这非常有效。谢谢你的洞察力@布里安托姆塞特-汤莱恩, 这和MarioAlexandroSantin提出并删除的建议是一样的?。这不是一个重复,因为这个问题似乎围绕着system-V IPC。注意到并收回。谢谢
cmd := exec.Command("dc")
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
cmd.Start()

stdin.Write([]byte("12 34 +p\n"))

out := make([]byte, 1024)
n, _ := stdout.Read(out)

fmt.Println("OUTPUT:", string(out[:n]))

// prints "OUTPUT: 46"