revise dynamic total example
Vladimir Bauer
7 years ago
| 26 | 26 | for { |
| 27 | 27 | n, err := read() |
| 28 | 28 | total += int64(n) |
| 29 | time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10) | |
| 30 | bar.IncrBy(n) | |
| 31 | 29 | if err == io.EOF { |
| 32 | // total is known, final=true | |
| 33 | bar.SetTotal(total, true) | |
| 34 | 30 | break |
| 35 | 31 | } |
| 36 | // total is unknown, final=false | |
| 32 | // while total is unknown, | |
| 33 | // set it to a positive number which is greater than current total, | |
| 34 | // to make sure no complete event is triggered by next IncrBy call. | |
| 37 | 35 | bar.SetTotal(total+2048, false) |
| 36 | bar.IncrBy(n) | |
| 37 | time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10) | |
| 38 | 38 | } |
| 39 | ||
| 40 | // force bar complete event, note true flag | |
| 41 | bar.SetTotal(total, true) | |
| 39 | 42 | |
| 40 | 43 | p.Wait() |
| 41 | 44 | } |