dtlygweb2017 2013-07-20 03:28
浏览 86
已采纳

在Golang中,http.HandleFunc是否被阻止?

i'm write a httpserver in Golang , but i find the http.HandleFunc will be block when multi request from the web browser. how can i do make the server handle multi request in the same time ? thanks.

my code is:

func DoQuery(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    fmt.Printf("%d path %s
", 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...
")
    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...")
}

I ran your code and everything worked as expected. I did two requests at the same time (curl localhost:9090/query) and they both finished 10 seconds later, together. Maybe the problem is elsewhere? Here's the command I used: time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) – tjameson

thakns

that's strange. when i request same url from chrome ,send two request not handle in the same time, but use cur test can handle in the same time. but when i send two request use different url, it's can be handle in the same time.

[root@localhost httpserver]# ./httpServer

server start working...

1374301593 path /query?form=chrome

1374301612 path /query?from=cur2

1374301614 path /query?from=cur1

1374301618 path /query?form=chrome

1374301640 path /query?form=chrome2

1374301643 path /query?form=chrome1

*1374301715 path /query?form=chrome

1374301725 path /query?form=chrome*

**1374301761 path /query?form=chrome1

1374301763 path /query?form=chrome2**

  • 写回答

1条回答 默认 最新

  • doukuiqian9911 2013-07-20 06:25
    关注

    Yes, the standard HTTP server will start a new goroutine for each request. You should be able to do thousands of requests in parallel depending on the operating system settings.

    Your browser might be limiting how many requests it will send to one server; be sure you are testing with a client that doesn't have that limitation/"optimization".

    Reliably Go docs explaining Http Server creates a new gorotine for each request: http://golang.org/pkg/net/http/#Server.Serve

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)