加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 19:22:43 所属栏目:大数据 来源:网络整理
导读:正如我从golang docs那里学到的,如果我用8个核心(intel i7)的cpu设置runtime.GOMAXPROCS(8),然后启动无限循环的goroutine,其他gorutines不应该被阻止,因为有很多线程和goprocs.但是在使用net / http包时不是这样,无限循环goroutine会在几次调用后阻塞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

服务器代码:

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

客户代码:

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

}

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

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

正如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可能会有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读