Created
June 7, 2011 01:44
-
-
Save kylelemons/1011514 to your computer and use it in GitHub Desktop.
Multiple workers in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wg := new(sync.WaitGroup) | |
for i := 0; i < workers; i++ { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
for { | |
select { | |
case input, open := <-In: | |
if !open { return } | |
v, e := f(input) | |
Out <- Result{v, e} | |
case <-quit: | |
return | |
} | |
} | |
}() | |
} | |
work <- blah | |
... | |
close(work) | |
wg.Wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment