FAQ

On Tuesday, November 18, 2014 6:39:27 PM UTC+2, Philip Thomas wrote:
I guess the main problem I am having is coordinating the shutdown of
multiple what I would call "queue workers" - basically one input channel,
one output channel, and about 1000 goroutines processing input, dropping
some data, and sending on the output channel. What's the standard pattern
for recognizing that the input channel has closed, then closing the output
channel when processing has finished without losing data (e.g. "ERROR -
output channel already closed"). I thought that the input shutdown message
could only be received by one queue worker, not all of them.
1. *Do* watch the video, and do read the article suggested above. They are
great.

2. For your specific question: Reading from a closed channel never blocks.
Instead, you get the zero-value for the channel's element type. If the
type's zero value is valid data in your application, you can check if a
channel is closed by receiving from it like this:

     v, ok := <- cin
     if ! ok {
         // channel closed
     }

You can receive as many times as you want from a closed channel (and by as
many goroutines as you want). All receives will return v == zero and ok ==
false.

Once a worker goroutine detects that its input channel is closed it should
do whatever cleanup necessary and then exit. It should *not* close the
output channel.

The output channel must be closed when *ALL* worker goroutines have
finished (because if any of them are still running they may have results to
send).

Another goroutine (*not* one of the workers, and most-likely the one that
started them) should monitor when all worker-goroutines have finished (e.g.
by waiting on a WaitGroup, or by some other means). When it detects that
they have, *it* closes the output channel.

/npat

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

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 9 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 17, '14 at 9:04p
activeNov 18, '14 at 11:04p
posts9
users7
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase