从Go

时间:2015-10-25 09:11:00

标签: go batch-processing channel

我正在循环中读取通道中的值,如下所示:

for {
    capturedFrame := <-capturedFrameChan
    remoteCopy(capturedFrame)
}

为了提高效率,我想批量阅读这些值,如下所示(伪代码):

for {
    capturedFrames := <-capturedFrameChan
    multipleRemoteCopy(capturedFrames)
}

但我不知道该怎么做。如果我多次拨打capturedFrames := <-capturedFrameChan,它将会阻止。

基本上,我想要的是阅读captureFrameChan中的所有可用值,如果没有,则会像往常一样阻塞。

在Go中实现这一目标的方法是什么?

5 个答案:

答案 0 :(得分:4)

这样的事情应该有效:

for {
    // we initialize our slice. You may want to add a larger cap to avoid multiple memory allocations on `append`
    capturedFrames := make([]Frame, 1)
    // We block waiting for a first frame
    capturedFrames[0] = <-capturedFrameChan

forLoop:
    for {
        select {
        case buf := <-capturedFrameChan:
            // if there is more frame immediately available, we add them to our slice
            capturedFrames = append(capturedFrames, buf)
        default:
            // else we move on without blocking
            break forLoop
        }
    }

    multipleRemoteCopy(capturedFrames)
}

答案 1 :(得分:1)

尝试以下操作(对于类型为ch的频道T):

for firstItem := range ch {                      // For ensure that any batch could not be empty
    var itemsBatch []T
    itemsBatch = append(itemsBatch, firstItem)
Remaining: 
    for len(itemsBatch) < BATCHSIZE {            // For control maximum size of batch
        select {
        case item := <-ch:
            itemsBatch = append(itemsBatch, item)
        default:
            break Remaining
        }
    }
    // Consume itemsBatch here...
}

但是,如果BATCHSIZE是常数,则此代码将更有效:

var i int
itemsBatch := [BATCHSIZE]T{}
for firstItem := range ch {                      // For ensure that any batch could not be empty
    itemsBatch[0] = firstItem
Remaining:
    for i = 1; i < BATCHSIZE; i++ {              // For control maximum size of batch
        select {
        case itemsBatch[i] = <-ch:
        default:
            break Remaining
        }
    }
    // Now you have itemsBatch with length i <= BATCHSIZE;
    // Consume that here...
}

答案 2 :(得分:0)

我最终完成了以下操作。基本上我已经使用len(capturedFrames)知道有多少帧可用,然后在一个循环中检索它们:

for {
    var paths []string
    itemCount := len(capturedFrames)
    if itemCount <= 0 {
        time.Sleep(50 * time.Millisecond)
        continue
    }

    for i := 0; i < itemCount; i++ {
        f := <-capturedFrames
        paths = append(paths, f)
    }

    err := multipleRemoteCopy(paths, opts)
    if err != nil {
        fmt.Printf("Error: could not remote copy \"%s\": %s", paths, err)
    }
}

答案 3 :(得分:0)

通过使用len(capturedFrames),您可以像下面这样进行操作:

for {
    select {
    case frame := <-capturedFrames:
        frames := []Frame{frame}
        for i := 0; i < len(capturedFrames); i++ {
           frames = append(frames, <-capturedFrames)
        }
        multipleRemoteCopy(frames)
    }
}

答案 4 :(得分:-1)

似乎你也可以只进行基准测试

for {
    capturedFrame := <-capturedFrameChan
    go remoteCopy(capturedFrame)
}

没有任何代码库重构,看它是否提高了效率。