如何根据文件描述符的数量限制go例程
我有一个程序可以执行对HTTP服务器的查询,每个请求都会得到一个goroutine。 我很快发现这对于MacOS来说太多了,因为文件描述符的限制是250 我想知道我是否可以限制goroutine的数量,或者阻止,直到有可用的goroutine,而不是失败 可能是一个包含250个goroutine的工作池,然后将其余的请求排队 你的想法是什么如何根据文件描述符的数量限制go例程,go,file-descriptor,goroutine,Go,File Descriptor,Goroutine,我有一个程序可以执行对HTTP服务器的查询,每个请求都会得到一个goroutine。 我很快发现这对于MacOS来说太多了,因为文件描述符的限制是250 我想知道我是否可以限制goroutine的数量,或者阻止,直到有可用的goroutine,而不是失败 可能是一个包含250个goroutine的工作池,然后将其余的请求排队 你的想法是什么 package main import "fmt" const ROUTINE_LIMIT = 250 func main() { chan
package main
import "fmt"
const ROUTINE_LIMIT = 250
func main() {
channelCounter := make(chan bool, ROUTINE_LIMIT)
for {
select {
//will add till 250 after that it will block
case channelCounter <- true:
fmt.Println("added channel")
go startRoutine(channelCounter)
}
}
}
func startRoutine(channelCounter chan bool) {
/*
do your stuff
*/
//free the channel
<-channelCounter
}
输入“fmt”
常数例行程序\u限值=250
func main(){
channelCounter:=制造(chan bool,常规限制)
为了{
挑选{
//将添加到250,之后将阻塞
case channelCounter是一个非常粗糙的例子。我希望这对你有用嘿!太棒了,似乎是一个答案,而不是评论