rm hang example
Vladimir Bauer
9 years ago
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "math/rand" | |
| 5 | "time" | |
| 6 | ||
| 7 | "github.com/vbauerster/mpb" | |
| 8 | ) | |
| 9 | ||
| 10 | const ( | |
| 11 | totalItem = 100 | |
| 12 | maxBlockSize = 10 | |
| 13 | ) | |
| 14 | ||
| 15 | func main() { | |
| 16 | decor := func(s *mpb.Statistics) string { | |
| 17 | str := fmt.Sprintf("%d/%d", s.Current, s.Total) | |
| 18 | return fmt.Sprintf("%8s", str) | |
| 19 | } | |
| 20 | ||
| 21 | p := mpb.New(nil) | |
| 22 | bar := p.AddBar(totalItem).PrependFunc(decor).AppendETA(-6) | |
| 23 | ||
| 24 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 25 | // Fallowing will hang, to prevent | |
| 26 | // use bar.InProgress() bool method | |
| 27 | // for i := 0; bar.InProgress(); i += blockSize { | |
| 28 | for i := 0; i < totalItem; i += blockSize { | |
| 29 | bar.Incr(blockSize) | |
| 30 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 31 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) | |
| 32 | } | |
| 33 | ||
| 34 | p.Stop() | |
| 35 | fmt.Println("stop") | |
| 36 | } |