Last active
May 22, 2019 13:52
-
-
Save leyafo/0bf3b71b14ed96d41d28303373b9c1fb to your computer and use it in GitHub Desktop.
Report mutiple go routine progress. If you wang report async goroutine schedule, you can use a channel to report the progress sequentially. Especilly if you want design a progressing bar.
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 ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
c := make(chan struct{}) | |
go func() { | |
processing := 0 | |
for range c { | |
processing++ | |
fmt.Printf("%d%% ", processing) | |
} | |
}() | |
wg := &sync.WaitGroup{} | |
wg.Add(100) | |
for i := 0; i < 100; i++ { | |
go func() { | |
time.Sleep(1 * time.Second) | |
c <- struct{}{} | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
close(c) | |
fmt.Println("task Done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment