I'm running into this WaitGroup vs. signal channel debate with a slightly
different type of problem.
I have a main function that spins off goroutines for processing and uses a
signal channel to track when the goroutines should return. If the signal
channel is closed, then the goroutines should return. Once all have
returned, I do additional processing in the main function.
However, I have an additional requirement, that if certain conditions are
met, all processing in goroutines should stop and we should immediately
move on in the main function.
I can trigger the channel close, but some goroutines are still processing,
so sometimes I get panics as they try to write to channels that had been
closed in the meantime. (I'm noticing panics even if I check the signal
channel right before writing to any other channel.)
I can't use a WaitGroup because I might not want to wait until all
goroutines are done.
Any ideas on what I'm missing?
Thank you,
Aki
On Wednesday, June 6, 2012 at 10:24:42 PM UTC-4, Jim Robinson wrote:
Hi folks,
I've been finding myself reaching for sync.WaitGroup for a program
I'm writing, and it feels wrong... I'm hoping for some feedback
from you experts.
I'm writing a program that walks through a deep directory tree, looking
for particular
file names. When it finds a match it chucks the path over the wall to
another
goroutine, which is responsible for cracking the file open and performing
some
analysis. Once that goroutine is done it chucks the analysis over the wall
to a goroutine that is responsbile for writing the results out to a log
file.
The goroutines are using buffered channels, and I've got multiple instances
of goroutines reading from the channels. I did this because I did want to
have some parallel execution.
All this works great, as long as the main() function is kept alive long
enough
to allow any submitted work to make its way through the goroutines. If my
program just waited in main() until it got an interrupt, it would
finish processing
the files w/o a problem. But I want the program to exit on its own, when
all
the work has completed.
To accomplish this I reached for sync.WaitGroup, but I'm wondering
if there is a more elegant way to accomplish this "wait until the channels
have cleared" problem.
Jim
--Hi folks,
I've been finding myself reaching for sync.WaitGroup for a program
I'm writing, and it feels wrong... I'm hoping for some feedback
from you experts.
I'm writing a program that walks through a deep directory tree, looking
for particular
file names. When it finds a match it chucks the path over the wall to
another
goroutine, which is responsible for cracking the file open and performing
some
analysis. Once that goroutine is done it chucks the analysis over the wall
to a goroutine that is responsbile for writing the results out to a log
file.
The goroutines are using buffered channels, and I've got multiple instances
of goroutines reading from the channels. I did this because I did want to
have some parallel execution.
All this works great, as long as the main() function is kept alive long
enough
to allow any submitted work to make its way through the goroutines. If my
program just waited in main() until it got an interrupt, it would
finish processing
the files w/o a problem. But I want the program to exit on its own, when
all
the work has completed.
To accomplish this I reached for sync.WaitGroup, but I'm wondering
if there is a more elegant way to accomplish this "wait until the channels
have cleared" problem.
Jim
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.