当启动无限循环的goroutine时,Golang http服务器阻塞

前端之家收集整理的这篇文章主要介绍了当启动无限循环的goroutine时,Golang http服务器阻塞前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
正如我从golang docs那里学到的,如果我用8个核心(intel i7)的cpu设置runtime.GOMAXPROCS(8),然后启动无限循环的goroutine,其他gorutines不应该被阻止,因为有很多线程和goprocs.但是在使用net / http包时不是这样,无限循环goroutine会在几次调用后阻塞http服务器.
任何人都可以帮忙解释原因吗?

>如果我评论“go infinite loop”行,在服务器后启动客户端,客户端将输出1000个星号;但如果我启用goroutine,客户端将在打印几个星号后阻止
>我试过在goroutine中添加runtime.LockOSThread(),似乎不起作用
>我的环境:osx 10.10,go版本go1.3.1 darwin / amd64

服务器代码

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "runtime"
  8. )
  9.  
  10. func myHandler(w http.ResponseWriter,req *http.Request) {
  11. w.Write([]byte("hello"))
  12. }
  13.  
  14. func infiniteloop() {
  15. for {
  16.  
  17. }
  18. }
  19.  
  20. func main() {
  21. // set max procs for multi-thread executing
  22. runtime.GOMAXPROCS(runtime.Numcpu())
  23.  
  24. // print GOMAXPROCS=8 on my computer
  25. fmt.Println("GOMAXPROCS=",runtime.GOMAXPROCS(-1))
  26. http.Handle("/",http.HandlerFunc(myHandler))
  27.  
  28. // uncomment below line cause server block after some requests
  29. // go infiniteloop()
  30. if err := http.ListenAndServe(":8280",nil); err != nil {
  31. log.Fatal(err)
  32. }
  33. }

客户代码

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6.  
  7. func getOnce() {
  8. if resp,err := http.Get("http://localhost:8280"); err != nil {
  9. fmt.Println(err)
  10. return
  11. } else {
  12. defer func() {
  13. if err := resp.Body.Close(); err != nil {
  14. fmt.Println(err)
  15. }
  16. }()
  17. if resp.StatusCode != 200 {
  18. fmt.Println("error codde:",resp.StatusCode)
  19. return
  20. } else {
  21. fmt.Print("*")
  22.  
  23. }
  24. }
  25. }
  26.  
  27. func main() {
  28. for i := 1; i < 1000; i++ {
  29. getOnce()
  30. if i%50 == 0 {
  31. fmt.Println()
  32. }
  33. }
  34.  
  35. }

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

  1. func infiniteloop() {
  2. // add LockOSThread will not help
  3. runtime.LockOSThread()
  4. for {
  5. }
  6. }

正如http://golang.org/pkg/runtime/#LockOSThread所述,空循环应该在独立线程中执行,而其他goroutine不应该受到busy循环的影响.我的理解有什么不对?

Go运行时的调度程序 is not fully pre-emptive at this time.转到1.2改进了事项 by occasionally calling into the scheduler on function calls,但是示例中的无限循环没有函数调用,所以这没有帮助.

对于无限循环处理程序的实际正文,您可能会看到更好的行为.或者,在这种情况下,手动拨打runtime.Gosched可能会有所帮助.

猜你在找的Go相关文章