cancel example update
Vladimir Bauer
9 years ago
| 17 | 17 | |
| 18 | 18 | func main() { |
| 19 | 19 | |
| 20 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | |
| 21 | defer cancel() | |
| 22 | ||
| 23 | p := mpb.New().WithContext(ctx) | |
| 24 | ||
| 20 | 25 | var wg sync.WaitGroup |
| 21 | ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) | |
| 22 | defer cancel() | |
| 23 | p := mpb.New().SetWidth(64).WithContext(ctx) | |
| 26 | total := 100 | |
| 27 | numBars := 3 | |
| 28 | wg.Add(numBars) | |
| 24 | 29 | |
| 25 | name1 := "Bar#1:" | |
| 26 | bar1 := p.AddBar(50). | |
| 27 | PrependName(name1, 0, mpb.DwidthSync|mpb.DidentRight). | |
| 28 | PrependETA(4, mpb.DwidthSync|mpb.DextraSpace). | |
| 29 | AppendPercentage(5, 0) | |
| 30 | wg.Add(1) | |
| 31 | go func() { | |
| 32 | defer wg.Done() | |
| 33 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 34 | for i := 0; i < 50; i++ { | |
| 35 | select { | |
| 36 | case <-ctx.Done(): | |
| 37 | return | |
| 38 | default: | |
| 30 | for i := 0; i < numBars; i++ { | |
| 31 | name := fmt.Sprintf("Bar#%d:", i) | |
| 32 | bar := p.AddBarWithID(i, int64(total)). | |
| 33 | PrependName(name, 0, mpb.DwidthSync|mpb.DidentRight). | |
| 34 | PrependETA(4, mpb.DwidthSync|mpb.DextraSpace). | |
| 35 | AppendPercentage(5, 0) | |
| 36 | go func() { | |
| 37 | defer func() { | |
| 38 | // fmt.Printf("%s done\n", name) | |
| 39 | wg.Done() | |
| 40 | }() | |
| 41 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 42 | for i := 0; i < total; i++ { | |
| 43 | select { | |
| 44 | case <-ctx.Done(): | |
| 45 | return | |
| 46 | default: | |
| 47 | } | |
| 39 | 48 | sleep(blockSize) |
| 40 | bar1.Incr(1) | |
| 49 | bar.Incr(1) | |
| 41 | 50 | blockSize = rand.Intn(maxBlockSize) + 1 |
| 42 | 51 | } |
| 43 | } | |
| 44 | }() | |
| 45 | ||
| 46 | bar2 := p.AddBar(100). | |
| 47 | PrependName("", 0, mpb.DwidthSync). | |
| 48 | PrependETA(4, mpb.DwidthSync|mpb.DextraSpace). | |
| 49 | AppendPercentage(5, 0) | |
| 50 | wg.Add(1) | |
| 51 | go func() { | |
| 52 | defer wg.Done() | |
| 53 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 54 | for i := 0; i < 100; i++ { | |
| 55 | select { | |
| 56 | case <-ctx.Done(): | |
| 57 | return | |
| 58 | default: | |
| 59 | sleep(blockSize) | |
| 60 | bar2.Incr(1) | |
| 61 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 62 | } | |
| 63 | } | |
| 64 | }() | |
| 65 | ||
| 66 | bar3 := p.AddBar(80). | |
| 67 | PrependName("Bar#3:", 0, mpb.DwidthSync|mpb.DidentRight). | |
| 68 | PrependETA(4, mpb.DwidthSync|mpb.DextraSpace). | |
| 69 | AppendPercentage(5, 0) | |
| 70 | wg.Add(1) | |
| 71 | go func() { | |
| 72 | defer wg.Done() | |
| 73 | blockSize := rand.Intn(maxBlockSize) + 1 | |
| 74 | for i := 0; i < 80; i++ { | |
| 75 | select { | |
| 76 | case <-ctx.Done(): | |
| 77 | return | |
| 78 | default: | |
| 79 | sleep(blockSize) | |
| 80 | bar3.Incr(1) | |
| 81 | blockSize = rand.Intn(maxBlockSize) + 1 | |
| 82 | } | |
| 83 | } | |
| 84 | }() | |
| 52 | }() | |
| 53 | } | |
| 85 | 54 | |
| 86 | 55 | wg.Wait() |
| 87 | 56 | p.Stop() |