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

  • Jan Mercl at Aug 14, 2014 at 9:42 am
    On Thu, Aug 14, 2014 at 11:27 AM, 小菜 wrote:

    The program correctly limits the number of _g_oroutines executing the
    getUrlTask function to at most 5 at any given moment.

    -j

    --
    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.
  • Dan Kortschak at Aug 14, 2014 at 9:42 am

    On Thu, 2014-08-14 at 02:27 -0700, 小菜 wrote:
    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 ?
    This is correct to get what you want, but
    The digital output is not 100。
    you don't wait for the goroutines to finish.




    --
    Omnes mundum facimus.

    Dan Kortschak <dan.kortschak@adelaide.edu.au>
    F9B3 3810 C4DD E214 347C B8DA D879 B7A7 EECC 5A40
    10C7 EEF4 A467 89C9 CA00 70DF C18F 3421 A744 607C

    --
    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.
  • 小菜 at Aug 14, 2014 at 9:44 am
    wait for the goroutines to finish.



    在 2014年8月14日星期四UTC+8下午5时27分30秒,小菜写道:
    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.
  • 小菜 at Aug 14, 2014 at 9:46 am
    how to wait for the goroutines to finish?

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



    >

    --
    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.
  • Jan Mercl at Aug 14, 2014 at 9:51 am

    On Thu, Aug 14, 2014 at 11:46 AM, 小菜 wrote:
    how to wait for the goroutines to finish?
    For example: http://play.golang.org/p/lpk1mq1PVd

    -j

    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
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 © 2021 Grokbase