如何在golang中函数开始运行时立即执行ticker

mrphzbgm  于 2022-12-25  发布在  Go
关注(0)|答案(3)|浏览(318)

我希望在“daily check”之前执行“weekly updated”,如下所示。这意味着“time.Time”应该立即放置“timeChan”,而不是在主函数开始运行时等待两秒钟以上才执行。
而结果应该是这样的

weekly updated
daily check
daily check
daily check
daily check
weekly updated
daily check
daily check
daily check
daily check
...

当然,我可以只打印“每周更新”第一次,但有没有一个优雅的方法?
代码如下所示

package main

import "time"
import "fmt"

func main() {
    var a int
    timeChan := time.NewTicker(time.Second * 2).C

    tickChan := time.NewTicker(time.Millisecond * 500).C

    for {
        select {
        case <-timeChan:
            fmt.Println("weekly updated")
            a = 1
        case <-tickChan:
            if a == 1 {
                fmt.Println("daily check")
            } else {
                fmt.Println("Not update?")
            }
        }
    }
}

结果如下

Not update?
Not update?
Not update?
weekly updated
daily check
daily check
daily check
daily check
weekly updated
daily check
daily check
daily check
daily check
...
u4vypkhs

u4vypkhs1#

在第一次time.Millisecond时设置weekly的股票代码。然后在第一次完成时更改它。

package main

import (
    "fmt"
    "time"
)

func main() {
    var a = 0

    ticker := time.NewTicker(1)
    timeChan := ticker.C
    tickChan := time.NewTicker(time.Millisecond * 500).C

    for {
        select {
        case <-timeChan:
            fmt.Println("weekly updated")
            if a == 0 {
                ticker.Stop()
                timeChan = time.NewTicker(time.Second * 2).C
            }
            a = 1
        case <-tickChan:
            if a == 1 {
                fmt.Println("daily check")
            } else {
                fmt.Println("Not update?")
            }
        default:
        }
    }
}

输出:

weekly updated
daily check
daily check
daily check
daily check
weekly updated
6tdlim6h

6tdlim6h2#

只要把工作放入函数中并调用它。

var a int
timeChan := time.NewTicker(time.Second * 2).C
tickChan := time.NewTicker(time.Millisecond * 500).C
f := func() {
    fmt.Println("weekly updated")
    a = 1
}
f()
for {
    select {
    case <-timeChan:
        f()
    case <-tickChan:
        if a == 1 {
            fmt.Println("daily check")
        } else {
            fmt.Println("Not update?")
        }
    }
}
oo7oh9g9

oo7oh9g93#

我知道这是一个迟来的回应,但我认为这可能会对某人有所帮助。
如果for语句的目的是在一个时间间隔内只触发一个函数/任务,那么您可以将工作逻辑放在select语句之前,然后使用case来触发for循环,这将导致工作逻辑在进入循环时运行。我不知道您团队的项目结构,所以如果您的select语句有许多其他的case不是要作为触发器,那就没用了,但我还是说了出来

package main

import (
    "context"
    "log"
    "os"
    "os/signal"
    "time"
)

func main() {
    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    weeklyTicker := time.NewTicker(2 * time.Second)
    dailyTicker := time.NewTicker(10 * time.Second)
    customTrigger := make(chan struct{})

    // Example of custom trigger at a random point
    go func() {
        time.Sleep(5 * time.Second)
        customTrigger <- struct{}{}
    }()

    weeklyRun := true
    for {
        if weeklyRun {
            log.Println("Run weekly logic")
            weeklyRun = false
        } else {
            log.Println("Run daily logic")
        }

        select {
        case <-ctx.Done():
            log.Println("interrupt received")
            return
        case <-dailyTicker.C:
            log.Println("weekly updated")
            weeklyRun = true
        case <-weeklyTicker.C:
            log.Println("daily check")
        case <-customTrigger:
            log.Println("custom check trigger")
        }
    }
}

输出:

2022/12/22 13:52:12 Run weekly logic
2022/12/22 13:52:14 daily check
2022/12/22 13:52:14 Run daily logic
2022/12/22 13:52:16 daily check
2022/12/22 13:52:16 Run daily logic
2022/12/22 13:52:17 custom check trigger
2022/12/22 13:52:17 Run daily logic
2022/12/22 13:52:18 daily check
2022/12/22 13:52:18 Run daily logic
2022/12/22 13:52:20 daily check
2022/12/22 13:52:20 Run daily logic
2022/12/22 13:52:22 daily check
2022/12/22 13:52:22 Run daily logic
2022/12/22 13:52:22 weekly updated
2022/12/22 13:52:22 Run weekly logic
^C2022/12/22 13:52:23 interrupt received

相关问题