dsuxcxqep31023992 2018-07-18 04:29
浏览 74
已采纳

使用数据库中的映射每隔X秒运行一次代码

Using golang's time.NewTicker, I am trying to run each record from a database (consisting of text and a timer, i.e the number of seconds before it repeats) but it is only repeating the first record it grabs

func LoadTimedCommands() map[string]time.Duration {
    database := InitializeDB()

    rows, _ := database.Query("SELECT TimedResponse, Timer from timedcommands")

    com := make(map[string]time.Duration)
    for rows.Next() {
        var TimedResponse string
        var Timer time.Duration
        rows.Scan(&TimedResponse, &Timer)
        com[TimedResponse] = Timer
    }
    return com
}

func TimedCommands(conn net.Conn, channel string, name string) {
    timedcoms := LoadTimedCommands()
    for k, v := range timedcoms {
        for range time.NewTicker(v * time.Second).C {
            BotSendMsg(conn, channel, k, name)
        }
    }
}

So if I have two records to use from: Sqlite data

Then the code should run the first record every 15 seconds, and the second every 10. But again, only the first one that is loaded into the map is run.

  • 写回答

1条回答 默认 最新

  • dongzhong8834 2018-07-18 07:48
    关注

    Your problem is in this function:

    func TimedCommands(conn net.Conn, channel string, name string) {
        timedcoms := LoadTimedCommands()
        for k, v := range timedcoms {
            for range time.NewTicker(v * time.Second).C {
                BotSendMsg(conn, channel, k, name)
            }
        }
    }
    

    In this section, where you range over the channel time.NewTicker(...).C:

    for range time.NewTicker(v * time.Second).C {
        BotSendMsg(conn, channel, k, name)
    }
    

    This will try and read values from this one ticker until the channel C is closed. This means that the outer loop only does one iteration, then we block on the inner loop until C is closed. And we never create more than one Ticker at once.

    You could get around this by creating tickers in their own goroutines:

    for k, v := range timedcoms {
        go func(conn net.Conn, channel, name, k string, v time.Duration) {
            for range time.NewTicker(v * time.Second).C {
                BotSendMsg(conn, channel, k, name)
            }
        }(conn, channel, k, v)
    }
    

    But you will probably want to include some synchronisation to ensure that you can stop the goroutines that you start, or to ensure that BotSendMsg(...) can be called from multiple goroutines at the same time etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了