dousha1394 2018-03-08 06:14
浏览 71
已采纳

如何允许多个对象从单个go子例程获取数据

I have a case where I want to spin up a go subroutine that will fetch some data from a source periodically. If the call fails, it stores the error until the next call succeeds. Now there are several instances in the code where a instance would access this data pulled by the go subroutine. How can I implement something like that?

UPDATE

I have had some sleep and coffee in me and I think I need to rephrase the problem more coherently using java-ish semantics.

I have come up with a basic singleton pattern that returns me a interface implementation that is running a go subroutine internally in a forever loop (lets put the cardinal sin of forever loops aside for a moment). The problem is that this interface implementation is being accessed by multiple threads to get the data collected by the go subroutine. Essentially, the data is pulled every 10 mins by the subroutine and then requested infinite number of times. How can I implement something like that?

  • 写回答

2条回答 默认 最新

  • doudai8083 2018-03-08 11:37
    关注

    Here's a very basic example of how you can periodically fetch and collect data.

    Have in mind: running this code will do nothing as main will return before anything really happens, but how you handle this depends on your specific use case. This code is really bare bones and needs improvements. It is a sketch of a possible solution to a part of your problem :)

    I didn't handle errors here, but you could handle them the same way fetched data is handled (so, one more chan for errors and one more goroutine to read from it).

    func main() {
    
        period := time.Second
        respChan := make(chan string)
        cancelChan := make(chan struct{})
        dataCollection := []string
    
        // periodicaly fetch data and send it to respChan
        go func(period time.Duration, respChan chan string, cancelChan chan struct{}) {
            ticker := time.Ticker(period)
            for {
                select {
                case <-ticker.C:
                    go fetchData(respChan)
                case <-cancelChan:
                    // close respChan to stop reading goroutine
                    close(respChan)
                    return
                }
            }
        }(period, cancelChan)
    
        // read from respChan and write to dataCollection
        go func(respChan chan string) {
            for data := range respChan {
                dataCollection = append(dataCollection, data)
            }
        }(respChan)
    
        // close cancelChan to gracefuly stop the app
        // close(cancelChan)
    }
    
    
    func fetchData(respChan chan string) {
        data := "fetched data"
        respChan <- data
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来