Golang http server 在启动无限循环的goroutine时阻塞

use*_*965 5 http block go goroutine

正如我从 golang 文档中了解到的,如果我将 runtime.GOMAXPROCS(8) 设置为 8 核(intel i7)的 cpu,然后启动一个无限循环的 goroutine,其他 gorutines 不应该被阻塞,因为有足够的线程和 goprocs。但是在使用 net/http 包时就不是这样了,一个无限循环的 goroutine 会在几次调用后阻塞 http 服务器。任何人都可以帮助解释原因吗?

  1. 如果我注释“无限循环”这一行,在服务器之后启动客户端,客户端将输出1000个星号;但是如果我启用 goroutine,客户端会在打印几个星号后阻塞
  2. 我已经尝试在 goroutine 中添加 runtime.LockOSThread() ,似乎不起作用
  3. 我的环境:osx 10.10, go version go1.3.1 darwin/amd64

服务器代码:

package main

import (
    "fmt"
    "log"
    "net/http"
    "runtime"
)

func myHandler(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello"))
}

func infiniteloop() {
    for {

    }
}

func main() {
    // set max procs for multi-thread executing
    runtime.GOMAXPROCS(runtime.NumCPU())

    // print GOMAXPROCS=8 on my computer
    fmt.Println("GOMAXPROCS=", runtime.GOMAXPROCS(-1))
    http.Handle("/", http.HandlerFunc(myHandler))

    // uncomment below line cause server block after some requests 
    // go infiniteloop()
    if err := http.ListenAndServe(":8280", nil); err != nil {
        log.Fatal(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

客户端代码:

package main
import (
    "fmt"
    "net/http"
)

func getOnce() {
    if resp, err := http.Get("http://localhost:8280"); err != nil {
        fmt.Println(err)
        return
    } else {
        defer func() {
            if err := resp.Body.Close(); err != nil {
                fmt.Println(err)
            }
        }()
        if resp.StatusCode != 200 {
            fmt.Println("error codde:", resp.StatusCode)
            return
        } else {
            fmt.Print("*")

        }
    }
}

func main() {
    for i := 1; i < 1000; i++ {
        getOnce()
        if i%50 == 0 {
            fmt.Println()
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我知道为什么这样的 emtpy 循环会阻塞其他 goroutine,但为什么runtime.LockOSThread()也无济于事?

package main

import (
    "fmt"
    "log"
    "net/http"
    "runtime"
)

func myHandler(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello"))
}

func infiniteloop() {
    for {

    }
}

func main() {
    // set max procs for multi-thread executing
    runtime.GOMAXPROCS(runtime.NumCPU())

    // print GOMAXPROCS=8 on my computer
    fmt.Println("GOMAXPROCS=", runtime.GOMAXPROCS(-1))
    http.Handle("/", http.HandlerFunc(myHandler))

    // uncomment below line cause server block after some requests 
    // go infiniteloop()
    if err := http.ListenAndServe(":8280", nil); err != nil {
        log.Fatal(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

正如http://golang.org/pkg/runtime/#LockOSThread提到的,空循环应该在一个独立的线程中执行,其他 goroutine 不应该受到繁忙循环的影响。我的理解有什么问题?

Jam*_*dge 5

Go 运行时的调度程序此时还不是完全先发制人的。Go 1.2通过偶尔在函数调用上调用调度程序来改进问题,但是您示例中的无限循环没有函数调用,因此这无济于事。

通过无限循环处理程序的实际主体,您可能会看到更好的行为。或者,runtime.Gosched在这种情况下,手动调用可能会有所帮助。