基本示例

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. now = time.Now()
  14. )
  15. gtimer.AddTimes(ctx, time.Second, 10, func(ctx context.Context) {
  16. fmt.Println(gtime.Now(), time.Duration(time.Now().UnixNano()-now.UnixNano()))
  17. now = time.Now()
  18. })
  19. select {}
  20. }

执行后,输出结果为:

  1. 2021-05-27 13:28:19 1.004516s
  2. 2021-05-27 13:28:20 997.262ms
  3. 2021-05-27 13:28:21 999.972ms
  4. 2021-05-27 13:28:22 1.00112s
  5. 2021-05-27 13:28:23 998.773ms
  6. 2021-05-27 13:28:24 999.957ms
  7. 2021-05-27 13:28:25 1.002468s
  8. 2021-05-27 13:28:26 997.468ms
  9. 2021-05-27 13:28:27 999.981ms
  10. 2021-05-27 13:28:28 1.002504s

单例任务

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. "github.com/gogf/gf/v2/os/glog"
  6. "github.com/gogf/gf/v2/os/gtimer"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. ctx = gctx.New()
  12. interval = time.Second
  13. )
  14. gtimer.AddSingleton(ctx, interval, func(ctx context.Context) {
  15. glog.Print(ctx, "doing")
  16. time.Sleep(5 * time.Second)
  17. })
  18. select {}
  19. }

执行后,输出结果为:

  1. 2021-11-14 11:50:42.192 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  2. 2021-11-14 11:50:48.190 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  3. 2021-11-14 11:50:54.192 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  4. 2021-11-14 11:51:00.189 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  5. ...

延迟任务

延迟任务是指在指定时间后生效的定时任务。我们可以通过 DelayAdd* 相关方法实现延迟任务的创建。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. delay = time.Second
  14. interval = time.Second
  15. )
  16. fmt.Println("Start:", gtime.Now())
  17. gtimer.DelayAdd(
  18. ctx,
  19. delay,
  20. interval,
  21. func(ctx context.Context) {
  22. fmt.Println("Running:", gtime.Now())
  23. },
  24. )
  25. select {}
  26. }

执行后,终端输出:

  1. Start: 2021-05-27 13:26:02
  2. Running: 2021-05-27 13:26:04
  3. Running: 2021-05-27 13:26:05
  4. Running: 2021-05-27 13:26:06
  5. Running: 2021-05-27 13:26:07
  6. Running: 2021-05-27 13:26:08
  7. Running: 2021-05-27 13:26:09
  8. Running: 2021-05-27 13:26:10
  9. Running: 2021-05-27 13:26:11
  10. ...

SetTimeoutSetInterval

这两个方法来源于 Javascript 常用定时方法。其中 SetTimeout 用于创建只执行一次的定时任务,不过可以通过递归调用 SetTimeout 来实现无限间隔执行。 SetIterval 用于创建间隔执行不退出的定时任务。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. timeout = time.Second
  14. interval = time.Second
  15. )
  16. gtimer.SetTimeout(ctx, timeout, func(ctx context.Context) {
  17. fmt.Println("SetTimeout:", gtime.Now())
  18. })
  19. gtimer.SetInterval(ctx, interval, func(ctx context.Context) {
  20. fmt.Println("SetInterval:", gtime.Now())
  21. })
  22. select {}
  23. }

执行后,终端输出:

  1. SetInterval: 2021-05-27 13:20:50
  2. SetTimeout: 2021-05-27 13:20:50
  3. SetInterval: 2021-05-27 13:20:51
  4. SetInterval: 2021-05-27 13:20:52
  5. SetInterval: 2021-05-27 13:20:53
  6. SetInterval: 2021-05-27 13:20:54
  7. SetInterval: 2021-05-27 13:20:55
  8. SetInterval: 2021-05-27 13:20:56
  9. SetInterval: 2021-05-27 13:20:57
  10. SetInterval: 2021-05-27 13:20:58
  11. ...

Exit 退出

我们可以在定时任务中通过 Exit 方法强制退出定时任务的继续执行,该定时任务将会被从定时器中移除。

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. )
  14. gtimer.SetInterval(ctx, time.Second, func(ctx context.Context) {
  15. fmt.Println("exit:", gtime.Now())
  16. gtimer.Exit()
  17. })
  18. select {}
  19. }

执行后,终端输出:

  1. exit: 2021-05-27 13:31:24