dqnhfbc3738
2018-06-06 14:34
浏览 327
已采纳

Golang中多个程序之间的交互

I'm trying to have two golang programs communicating using pipes, each running concurrently e.g:

1 go run master/main/main.go 

2 master/main/main.go calls slave.exe (built go program)

3 slave.exe prints out "Ping"

4 master/main/main.go reads "Ping" and writes "Pong"

5 slave.exe reads "Pong" and prints out "Message recieved: Pong"

The program reaches step 4 but it does not receive another message from slave.exe.

In master/main/main.go:

package main

import (
    "fmt"
    "os"
    "os/exec"
    "bufio"
)

func main() {
    // Run compiled slave project:
    c := exec.Command("main", insert_path_to_exe_here)

    out, _ := c.StdoutPipe()
    in, _ := c.StdinPipe()

    c.Start() // Using Start() instead of Run() because Run() waits for program to finish before moving on.

    inwriter := bufio.NewWriter(in)
    outreader := bufio.NewReader(out)

    // This should print "Ping".
    fmt.Println(outreader.ReadString('
'))

    inwriter.WriteString("Pong")

    // This should print "Message received: Pong"
    fmt.Println(outreader.ReadString('
'))

}

In slave/main/main.go:

package main

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

func main() {
    fmt.Println("Ping")
    reader := bufio.NewReader(os.Stdin)

    s, _ := reader.ReadString('
')

    fmt.Println("Message received: ", s)
}

To run:

  • Build slave/main/main.go
  • Replace insert_path_to_exe_here with the path to the built executable and replace "main" with the name of the executable.
  • Run master/main/main.go

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使两个golang程序使用管道进行通信,每个管道同时运行,例如:

  1进入运行master / main / main.go 
 
2 master / main / main.go调用slave.exe(内置go程序)
 
3 slave.exe打印出“ Ping”  
 
4 master / main / main.go读取“ Ping”并写入“ Pong” 
 
5 slave.exe读取“ Pong”并打印出“收到的消息:Pong” 
    
 
 

程序到达步骤4,但没有收到来自slave.exe的另一条消息。

在master / main / main.go中:

 包main 
 
import(
“ fmt” 
“ os  “ 
” os / exec“ 
” bufio“ 
)
 
func main(){
 //运行已编译的从属项目:
c:= exec.Command(” main“,insert_path_to_exe_here)
 \  n out,_:= c.StdoutPipe()
输入,_:= c.StdinPipe()
 
 c.Start()//使用Start()而不是Run()因为Run()等待程序 
 
写入者:= bufio.NewWriter(in)
外发阅读器:= bufio.NewReader(out)
 
 //应该打印“ Ping”。
 fmt.Println(outreader  .ReadString('
'))
 
 inwriter.WriteString(“ Pong”)
 
 //应该显示“收到的消息:Pong” 
 fmt.Println(outreader.ReadString('
'  ))
 
} 
   
 
 

在slave / main / main.go中:

  package main \  n 
import(
“ fmt” 
“ bufio” 
“ os” 
)
 
func main(){
 fmt.Println(“ Ping”)
 reader:= bufio.NewReader(os  .Stdin)
 
s,_:= reader.ReadString('
')
 
 fmt.Println(“收到的消息:”,s)
}  
   
 
 

要运行:

  • 构建slave / main / main.go
  • 将insert_path_to_exe_here替换为生成的可执行文件的路径,并用可执行文件的名称替换“ main”。
  • 运行master / main / main.go
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbo5230 2018-06-06 14:48
    已采纳

    Your subprocess is using ReadString(' '), but you aren't writing a character, nor are you flushing the buffered writer.

    This will write the expected data to the pipe:

    inwriter.WriteString("Pong
    ")
    inwriter.Flush()
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题