小编use*_*460的帖子

在Golang中,是http.HandleFunc阻止吗?

我在Golang中写了一个httpserver,但是我发现当来自Web浏览器的多个请求时,http.HandleFunc将被阻塞.如何让服务器同时处理多个请求?谢谢.

我的代码是:

func DoQuery(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    fmt.Printf("%d path %s\n", time.Now().Unix(), r.URL.Path)
    time.Sleep(10 * time.Second)
    fmt.Fprintf(w, "hello...")
    //why this function block when multi request ?
}

func main() {
    fmt.Printf("server start working...\n")
    http.HandleFunc("/query", DoQuery)
    s := &http.Server{
        Addr:         ":9090",
        ReadTimeout:  30 * time.Second,
        WriteTimeout: 30 * time.Second,
        //MaxHeaderBytes: 1 << 20,
    }
    log.Fatal(s.ListenAndServe())
    fmt.Printf("server stop...")
}
Run Code Online (Sandbox Code Playgroud)

我运行了你的代码,一切都按预期工作.我同时做了两个请求(卷曲localhost:9090 /查询),他们都在10秒后完成了.也许问题出在其他地方?这是我使用的命令:time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) - tjameson

thakns

真奇怪.当我从chrome请求相同的url时,同时发送两个请求不处理,但是使用cur test可以同时处理.但是当我发送两个请求使用不同的url时,它可以在同一时间处理.

[root @ localhost httpserver]#./ httppServer

服务器开始工作......

1374301593路径/查询?form …

block go httpserver

6
推荐指数
1
解决办法
6174
查看次数

标签 统计

block ×1

go ×1

httpserver ×1