forked from vbauerster/mpb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (57 loc) · 1.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//+build go1.7
package main
import (
"context"
"fmt"
"math/rand"
"sync"
"time"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
const (
maxBlockSize = 12
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var wg sync.WaitGroup
p := mpb.New(
mpb.WithWaitGroup(&wg),
mpb.WithContext(ctx),
)
total := 100
numBars := 3
wg.Add(numBars)
for i := 0; i < numBars; i++ {
name := fmt.Sprintf("Bar#%d:", i)
bar := p.AddBar(int64(total), mpb.BarID(i),
mpb.PrependDecorators(
decor.StaticName(name, 0, decor.DwidthSync|decor.DidentRight),
decor.ETA(4, decor.DSyncSpace),
),
mpb.AppendDecorators(
decor.Percentage(5, 0),
),
)
go func() {
defer wg.Done()
blockSize := rand.Intn(maxBlockSize) + 1
for i := 0; i < total; i++ {
select {
case <-ctx.Done():
return
default:
}
sleep(blockSize)
bar.Incr(1)
blockSize = rand.Intn(maxBlockSize) + 1
}
}()
}
p.Stop()
fmt.Println("stop")
}
func sleep(blockSize int) {
time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
}