Last active
December 9, 2016 08:21
-
-
Save heppu/adf35c4906cc6c4530fadbf5f3e35784 to your computer and use it in GitHub Desktop.
Limit parallel goroutines with buffered channel.
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
package main | |
import ( | |
"log" | |
"sync" | |
"time" | |
) | |
const Limit = 5 | |
func main() { | |
log.SetFlags(log.Ltime) // format log output hh:mm:ss | |
var wg sync.WaitGroup | |
workers := make(chan struct{}, Limit) | |
doWork := func(i int, j string) { | |
defer wg.Done() | |
time.Sleep(2 * time.Second) | |
log.Printf("Worker %d working on %s\n", i, j) | |
<-workers | |
} | |
for j := 0; j < 15; j++ { | |
work := string(rune(97 + j)) | |
log.Printf("Work %s enqueued\n", work) | |
workers <- struct{}{} | |
wg.Add(1) | |
go doWork(j, work) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is true, I just got interested how one could achieve same thing using buffered channel instead of worker pool and got little drifted from original use case. =)