Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

go - Golang http server blocks when starts a goroutine of infinite-loop

As i learned from golang docs, if i set runtime.GOMAXPROCS(8) with a cpu of 8 cores (intel i7), then start a goroutine of infinite-loop, other gorutines should not be blocked because there are engough threads and goprocs. But this is not true when using net/http package, an infinite-loop goroutine will block http server after a few invocations. Can anyone help to explain why ?

  1. If i comment the line of "go infinite loop", start client after server, client will output 1000 asterisks; but if i enable the goroutine, client will block after print a few asterisks
  2. I have tried add runtime.LockOSThread() in the goroutine, it seems that doesn't work
  3. My Environment: osx 10.10, go version go1.3.1 darwin/amd64

Server code:

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

Client code:

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

}

Now i know why such emtpy loop block other goroutines, but why runtime.LockOSThread() doesn't help either?

func infiniteloop() {
    // add LockOSThread will not help
    runtime.LockOSThread()
    for {
    }
}

As http://golang.org/pkg/runtime/#LockOSThread mentioned, the empty loop should be executed in an standalone thread, and other goroutines should not be impacted by the busy loop. What's wrong in my understanding?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...