FAQ
How to limit the number of coroutines in golang? Is necessary or unnecessary
?

See a lot of articles said if you want to limit the number of coroutines on
the net, can take the following methods.

But this method is not correct.So what is the right way ?


Go Playground :http://play.golang.org/p/HcZ5h5YmhL

-------------------------------------------------------------------------------------------------------------------


package main

import (
"fmt"
"runtime"
)

var c = make(chan bool, 5)

func main() {
getUrl()
}

func getUrl() {
for i := 1; i <= 100; i++ {
c <- true
go getUrlTask(i)
}
}

func getUrlTask(i int) {
fmt.Println(i)
fmt.Printf("NumGoroutine: %d\n", runtime.NumGoroutine())
<-c
}

The digital output is not 100。




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 6 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 14, '14 at 9:27a
activeAug 14, '14 at 9:51a
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase