Always have x number of goroutines running at any time

前端 未结 7 1798
鱼传尺愫
鱼传尺愫 2020-12-13 00:10

I see lots of tutorials and examples on how to make Go wait for x number of goroutines to finish, but what I\'m trying to do is have ensure there are always x number running

7条回答
  •  [愿得一人]
    2020-12-13 00:54

    This is a simple producer-consumer problem, which in Go can be easily solved using channels to buffer the paquets.

    To put it simple: create a channel that accept your IDs. Run a number of routines which will read from the channel in a loop then process the ID. Then run your loop that will feed IDs to the channel.

    Example:

    func producer() {
        var buffer = make(chan uint)
    
        for i := 0; i < 20; i++ {
            go consumer(buffer)
        }
    
        for _, id :=  range IDs {
            buffer <- id
        }
    }
    
    func consumer(buffer chan uint) {
        for {
            id := <- buffer
            // Do your things here
        }
    }
    

    Things to know:

    • Unbuffered channels are blocking: if the item wrote into the channel isn't accepted, the routine feeding the item will block until it is
    • My example lack a closing mechanism: you must find a way to make the producer to wait for all consumers to end their loop before returning. The simplest way to do this is with another channel. I let you think about it.

提交回复
热议问题