douping7105 2012-10-19 06:27
浏览 30
已采纳

Go:通过管道发送gob挂起-更新:进程外http.ResponseWriter正在阻止

I'm writing a webserver that distributes requests to out-of-process programs in Go. I'm sending the ResponseWriter and Request datatypes through Pipes using gob.

The problem is the external process is hanging when receiving the gob.

UPDATE The gob is now successfully being sent to the external process, but now the external process is blocking at fmt.Fprintf(request.Resp, "Hello") and freezes there.

dispreq.go

package dispreq

import (
    "net/http"
)

type DispReq struct {
    Resp    http.ResponseWriter
    Req *http.Request
}

dispatcher.go

package main

import (
    "encoding/gob"
    "fmt"
    "net/http"
    "os"
    "os/exec"

    "dispreq"
)

func dispatch(w http.ResponseWriter, r *http.Request) {
    process := exec.Command("./hello")
    pipe, piperr := process.StdinPipe()

    if piperr != nil {
        fmt.Fprintf(os.Stderr, piperr.Error())
        return
    }

    encoder := gob.NewEncoder(pipe)

    process.Stdout = os.Stdout

    //UPDATE: encoder.Encode(&dispreq.DispReq{w, r})
    //UPDATE: process.Start()
    process.Start()
    encoder.Encode(&dispreq.DispReq{w, r})
    pipe.Close()
    process.Wait()
}

func main() {
    http.HandleFunc("/", dispatch)
    http.ListenAndServe(":8080", nil)
}

hello.go

package main

import (
    "dispreq"
    "encoding/gob"
    "os"

    "fmt"
)

func main() {
    gobDecoder := gob.NewDecoder(os.Stdin)
    var request dispreq.DispReq
    gobDecoder.Decode(&request)
    fmt.Fprintf(request.Resp, "Hello")
}
  • 写回答

1条回答 默认 最新

  • douyu53265 2012-10-19 07:26
    关注

    You should start the process (process.Start()) before sending data to it (encoder.Encode(&dispreq.DispReq{w, r})). You might also need to flush your pipe by closing it (pipe.Close()) or sending a .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗