Parallel channel send / receive go

I have a go channel called queue

and let 100 as the buffer size. Many of the subroutines can send data to this channel, while another procedure goes to receive data from this channel. This is a lengthy process, which means that the pipe acts as a conduit, absorbing data from many ends and plunging data into one end. I am doing something like this in the intake procedure:

for {
    for data := range queue {
        sink(data)
    }
} 

      

Now my question is, what if some new data was sent to the pipe buffer before the end of the range loop. Will new data be available for the next cycle of the range? Or will they be skipped if concurrency is not taken into account in this case?

+3


source to share


2 answers


You only need one cycle for

. From the spec for range expressions :

For channels, the iteration values ​​are the sequential values ​​sent over the channel until the channel is closed. If channel nil

, the range expression will block forever.



In this case, the range loop does not act like a regular range, such as a slice. Instead, when an element can be read from the pipe, the body of the loop processes it. Therefore, your nested loops should be replaced with the following:

for data := range queue {
    sink(data)
}

      

+4


source


As @Tim said , you only need one for

as it range

will emit values ​​from the channel until it is closed.

In general, the template you are describing is called fan-in . An example of a basic producer / consumer setup can be found here: http://play.golang.org/p/AhQ012Qpwj . The range loop works for the consumer:



// consumer acts as fan in, signals when it is done.
func consumer(out chan string, done chan bool) {
    for value := range out {
        fmt.Println(value)
    }
    done <- true
}

      

+3


source







All Articles