Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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:发送gob';管道上的s挂起-更新:进程外http.ResponseWriter被阻止_Go_Gob - Fatal编程技术网

Go:发送gob';管道上的s挂起-更新:进程外http.ResponseWriter被阻止

Go:发送gob';管道上的s挂起-更新:进程外http.ResponseWriter被阻止,go,gob,Go,Gob,我正在编写一个Web服务器,它将请求分发到Go中的进程外程序。我使用gob通过管道发送ResponseWriter和Request数据类型 问题是接收采空区时外部流程挂起 更新gob现在已成功发送到外部进程,但现在外部进程在fmt.Fprintf(request.Resp,“Hello”)处阻塞并冻结 dispreq.go package dispreq import ( "net/http" ) type DispReq struct { Resp http.Resp

我正在编写一个Web服务器,它将请求分发到Go中的进程外程序。我使用gob通过管道发送ResponseWriter和Request数据类型

问题是接收采空区时外部流程挂起

更新gob现在已成功发送到外部进程,但现在外部进程在
fmt.Fprintf(request.Resp,“Hello”)
处阻塞并冻结

dispreq.go

package dispreq

import (
    "net/http"
)

type DispReq struct {
    Resp    http.ResponseWriter
    Req *http.Request
}
调度员,开始

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)
}
你好,走吧

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")
}

在向进程发送数据之前,应该先启动进程(
process.start()
)(
encoder.Encode(&dispreq.dispreq{w,r})
)。您可能还需要通过关闭管道(
pipe.Close()
)或发送
\n

来冲洗管道。这就是问题所在,谢谢。但是现在我遇到了一个新问题,hello.go在fmt.Fprintf(request.Resp,“hello”)上阻塞。不可能将ResponseWriter传递给单独的进程吗?这是因为
编码/gob
无法对文件描述符进行编码(文档是)