Help us understand the problem. What is going on with this article?

errgroupとsyncでワーカープールっぽいものを実装する

More than 1 year has passed since last update.

errgroupでgoroutineのエラーハンドリングを簡単にやれるのにプラスして、一緒にsync.WaitGroupを使うと起動したgoroutineで並列処理したデータをgochannelでプーリングするような実装もできるようになる。

ポイントは、データを蓄積する resultPool を Buffered Channel にすることで、データを溜められるようにすること。下の例では goroutine とそこから生成される UUID の数の乗数として 4 x 4 = 16 に指定している。

package main

import (
    "fmt"
    "github.com/satori/go.uuid"
    "golang.org/x/sync/errgroup"
    "sync"
)

func main() {
    e := errgroup.Group{}
    wg := &sync.WaitGroup{}
    resultPool := make(chan uuid.UUID, 16) // Buffered Channel
    inputs := []int{4, 4, 4, 4}

    for _, v := range inputs {
        wg.Add(1)
        v := v

        // UUIDを生成させる処理をgoroutineで行わせる。
        // 生成されたものはresultPoolに溜めていく
        e.Go(func() error {
            fmt.Printf("Worker running... to get %d hashes.\n", v)
            for i := 0; i < v; i++ {
                select {
                case resultPool <- uuid.Must(uuid.NewV4(), nil):
                default:
                }
            }
            wg.Done()
            return nil
        })
    }

    if err := e.Wait(); err != nil {
        println(err)
        return
    }

    wg.Wait()
    close(resultPool)

    // resultPoolに溜まったデータを出力
    fmt.Println("\n[Results]")
    for v := range resultPool {
        fmt.Printf("%s\n", v)
    }
}
実行結果
 $ go run test_errgroup_with_waitgroup.go
Worker running... to get 4 hashes.
Worker running... to get 4 hashes.
Worker running... to get 4 hashes.
Worker running... to get 4 hashes.

[Results]
585bb7ce-f949-4bf7-915c-4310f70a3b6b
9a6d8245-d3d6-4bac-87ca-38ec801865db
486cf512-bc93-49ce-9acb-95378dc73a28
4ba4fc82-8a80-42e7-97b9-2e7b33cad5f6
91c8518e-48d7-4d13-8906-c11c9e049e72
4aa1330f-7dbb-4c22-990c-c7254fc3512e
a22cefeb-060e-4a72-ac5f-5ae133323765
0af8bed4-e6c9-405c-81e7-0e56dd6ad6e7
943d247e-7cc0-4ba8-8618-a25f3ac10afa
dceedb13-46f7-491e-9f51-c5f7691b6030
186a1799-8bee-4907-8dca-8a919cbd9b13
03e86639-a115-423e-917b-d6185bf8664a
072cacb2-38ee-4bcb-a78a-068fccdf544b
6a6c040b-8a9d-47d7-a017-01bbfa1b5463
6b57c670-2e89-400d-a62f-941157f934cd
4e01d472-5885-4f22-bfdf-750440adafaf
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした