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条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值