doufang7385 2016-08-09 13:14
浏览 39

Go语言:向生成的进程发送数据时,Wait()不返回

I need to send several MB to a spawned Go process. I followed the directions at https://gobyexample.com/spawning-processes, but my data is too large to send that way. I thought I could chunk the data. Here is the code I ended up with:

doit := exec.Command("/bin/bash", "-c", cmd)    // cmd reads stdin
doit_stdin,_  := doit.StdinPipe()
doit_stdout,_ := doit.StdoutPipe()

doit.Start()

go func() {
  w := bufio.NewWriter(doit_stdin)
  for i := 0; i < nchunks; i++ {
    w.Write(input_data[chunk_begin[i]:chunk_begin[i+1]]) 
    w.Flush()
  }
  doit_stdin.Close()
}()

rc,_ := ioutil.ReadAll(doit_stdout)
doit.Wait()

Where input_data is []byte and chunk_begin is an array of break points computed elsewhere. Everything works just fine. cmd gets all the data sent to it, detects EOF, sends results back and exits. ReadAll gets all the info sent back from cmd. But do it.Wait() blocks. I cannot understand why doit.Wait is blocking.

Or if there is some other standard way of transferring larger amounts of data, that would be fine too. I do need to control the break points because the data has internal structure.

Any help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示