doujingxi3356 2017-08-13 13:17
浏览 41
已采纳

Goroutine,回调和sync.WaitGroup

With the following code:

package main

import (
    "github.com/davecgh/go-spew/spew"
    "sync"
    "time"
)
func callbackWithTimeout(cbFunc func() ([]byte, error), timeout time.Duration) {
    defer wg.Done() //I don't want this function to know about sync.WaitGroup
    time.Sleep(timeout)
    d, e := cbFunc()
    spew.Dump(d)
    spew.Dump(e)
}
var wg sync.WaitGroup
func main() {
    wg.Add(1)
    go func() {
        cbFunc := func() ([]byte, error) {
            //I feel like I should be able to defer here instead
            return nil, nil
        }
        callbackWithTimeout(cbFunc, time.Duration(4*time.Second))
    }()
    println("some line")
    wg.Wait()
}

In function callbackWithTimeout, I don't want to use defer wg.Done() because it's not callbackWithTimeout()'s concern to wg.Done(). How do I go about implementing such a thing? i.e., remove any sync.WaitGroup in callbackWithTimeout? I have a bit of problem understanding the separation of concerns here as a callback'er function should not have to know about waitgroups but in this case it seems, I have no other choice?

I feel like it should be a caller's responsibility to wg.Done() (which in this case is the cbFunc) but lack any concise reference to documentation or ideas on how to implement it in Go because by definition, all a callback function does is call the function back. So, where I am doing it wrong?


Silly assumptions were made by yours truly during refactoring. Working code below. Many thanks.

package main

import (
    "errors"
    "github.com/davecgh/go-spew/spew"
    "sync"
    "time"
)

func callbackWithTimeout(cbFunc func() ([]byte, error), timeout time.Duration) {
    time.Sleep(timeout)
    d, e := cbFunc()
    spew.Dump(d)
    spew.Dump(e)
}

func main() {
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        callbackWithTimeout(func() ([]byte, error) {
            b := []byte{1, 2, 3, 4}
            e := errors.New("error123")
            return b, e
        }, time.Duration(2*time.Second))
    }()
    println("some line")
    wg.Wait()
}
  • 写回答

1条回答 默认 最新

  • dqotv26286 2017-08-13 13:43
    关注

    May be like this?

    package main
    
    import (
        "sync"
        "time"
    
        "github.com/davecgh/go-spew/spew"
    )
    
    func callbackWithTimeout(cbFunc func() ([]byte, error), timeout time.Duration) {
        time.Sleep(timeout)
        d, e := cbFunc()
        spew.Dump(d)
        spew.Dump(e)
    }
    
    func main() {
        var wg sync.WaitGroup
    
        wg.Add(1)
    
        go func() {
            defer wg.Done() // move it here
            cbFunc := func() ([]byte, error) {
                //I feel like I should be able to defer here instead
                return nil, nil
            }
            callbackWithTimeout(cbFunc, time.Duration(4*time.Second))
        }()
    
        println("some line")
    
        wg.Wait()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路