4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Golangでマルチプログレスバーを表示したい!!

Posted at

Golangの十八番と言えば並行処理なわけですが、それぞれの処理がどのくらいの進捗なのか知りたい!っということで、pbとuiprogressと言う人気のパッケージを使って動かしてみました。

Golangのプログレスバーを実装するパッケージについては、こちらの記事で紹介しています。
Golangでプログレスバーを表示するためのパッケージ3選

pbでやってみた

main.go
package main

import (
    "log"
    "math/rand"
    "sync"
    "time"

    "github.com/cheggaaa/pb"
)

func processing(wg *sync.WaitGroup, b *pb.ProgressBar) {
    defer wg.Done()
    for n := 0; n < 200; n++ {
        b.Increment()
        rand.Seed(time.Now().UnixNano())
        time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
    }
    b.Finish()
}

func main() {
    process_1 := pb.New(200).Prefix("process_1: ").SetMaxWidth(80)
    process_2 := pb.New(200).Prefix("process_2: ").SetMaxWidth(80)
    process_3 := pb.New(200).Prefix("process_3: ").SetMaxWidth(80)

    pool, err := pb.StartPool(process_1, process_2, process_3)
    if err != nil {
        log.Fatal(err)
    }

    var wg sync.WaitGroup
    for _, bar := range []*pb.ProgressBar{process_1, process_2, process_3} {
        wg.Add(1)
        go processing(&wg, bar)
    }
    wg.Wait()
    pool.Stop()
}

実行してみるとこんな感じ
pb_multi.gif

uiprogressでやってみた

main.go
package main

import (
    "math/rand"
    "sync"
    "time"

    "github.com/gosuri/uiprogress"
    "github.com/gosuri/uiprogress/util/strutil"
)

func processing(process_name string, wg *sync.WaitGroup, u *uiprogress.Bar) {
    defer wg.Done()
    u.PrependFunc(func(b *uiprogress.Bar) string {
		return strutil.Resize(process_name + ": ", 11)
	})
    for u.Incr() {
        rand.Seed(time.Now().UnixNano())
        time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
    }
}

func main() {
    uiprogress.Start()
    process_1 := uiprogress.AddBar(100).AppendCompleted().PrependElapsed()
    process_2 := uiprogress.AddBar(300).AppendCompleted().PrependElapsed()
    process_3 := uiprogress.AddBar(200).AppendCompleted().PrependElapsed()

    var wg sync.WaitGroup

    wg.Add(1)
    go processing("process_1", &wg, process_1)
    wg.Add(1)
    go processing("process_2", &wg, process_2)
    wg.Add(1)
    go processing("process_3", &wg, process_3)

    wg.Wait()
}

実行してみるとこんな感じ
uiprogress_multi.gif

まとめ

なんか実際動かしてみると、Dockerでイメージをpullする時みたいでエモいですねw

どちらのパッケージも使いやすいので、馴染みのいい方を選んでカスタムするといいと思います。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?