forked from vbauerster/mpb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (51 loc) · 1.04 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
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
const (
maxBlockSize = 12
)
func main() {
var wg sync.WaitGroup
p := mpb.New(mpb.WithWaitGroup(&wg))
total := 100
numBars := 3
wg.Add(numBars)
for i := 0; i < numBars; i++ {
var name string
if i != 1 {
name = fmt.Sprintf("Bar#%d:", i)
}
b := p.AddBar(int64(total),
mpb.PrependDecorators(
decor.StaticName(name, 0, decor.DwidthSync),
decor.CountersNoUnit("%d / %d", 10, decor.DSyncSpace),
),
mpb.AppendDecorators(
decor.ETA(3, 0),
),
)
go func() {
defer wg.Done()
for blockSize, i := 0, 0; i < total; i++ {
blockSize = rand.Intn(maxBlockSize) + 1
if i&1 == 1 {
priority := total - int(b.Current())
p.UpdateBarPriority(b, priority)
}
b.Increment()
sleep(blockSize)
}
}()
}
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)))))
}