当前位置:网站首页>golang中的定时器
golang中的定时器
2022-06-12 10:18:00 【love666666shen】
在golang程序中,可以使用crontab进行定时任务处理,也可以通过Timer或Ticker简单地进行定时任务设置。
Timer和Ticker定时器的区别为:
使用Timer定时器时,需要调用Reset方法对定时器事件进行重置;而Ticker定时不需要调用Reset。
Timer定时任务示例:
func process() {
interval := 6 * time.Second
timer := time.NewTimer(interval)
ctx, _ := context.WithTimeout(ctx, 2 * time.Second)
// 定时任务处理逻辑
processLogic()
for {
// 调用Reset方法对timer对象进行定时器重置
timer.Reset(interval)
select {
case <-ctx.Done():
// 超时处理
log.Info("process timeout, exit")
return
case <-timer.C:
// 定时任务处理逻辑
processLogic()
}
}
}
Ticker定时任务,不需要Reset重置定时器。Ticker定时任务示例如下:
......
ticker := time.NewTicker(time.Second)
ctx, _ := context.WithTimeout(ctx, 2 * time.Second)
for {
select {
case <-ticker.C:
// 定时任务处理逻辑
...
case <-ctx.Done():
fmt.Println("request timeout")
// 结束定时任务
return
}
}
......
边栏推荐
- Checkpoint of the four cornerstones of Flink
- HALCON联合C#检测表面缺陷——仿射变换(三)
- Auto. JS debugging: use the network mode of lightning simulator for debugging
- Pandorabox uses firewall rules to define non internet time
- MYSQL的最左匹配原则的原理讲解
- Strange error -- frame detected by contour detection, expansion corrosion, and reversal of opening and closing operation effect
- 不需要安装防毒软件的系统Chromebook
- [DDS] ddsi-rtps specification
- [MySQL] learn more about the clustered indexes and auxiliary indexes (b+ tree indexes) in InnoDB
- Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
猜你喜欢

np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍

Code types and data structures corresponding to the five object types of redis

Introduction to IOT
![[Wayland] Weston startup process analysis](/img/0e/24f374144ea7ea762a83f6598ebd48.jpg)
[Wayland] Weston startup process analysis

Auto. JS debugging: use the network mode of lightning simulator for debugging

Create simple windowing programs using Visual Studio 2017

Autojs learning notes 6:text (txt) Findone() will report an error when switching apps. Finally, solve the implementation effect and switch any app until the script finds the control with the specified

Remote desktop cannot copy and paste solution
![[DDS] ddsi-rtps specification](/img/fe/16b835e3e4a8ff71ab3dbc4b9c4d2a.jpg)
[DDS] ddsi-rtps specification

容器江湖的爱恨情仇
随机推荐
Clickhouse column basic data type description
Detailed explanation and use of redis data types: key and string types
FPGA VGA display based on de2-115 platform
CLAHE in opencv for 16 bit image enhancement display
JVM (V) Virtual machine class loading (parental delegation mechanism)
C# break continue return 三者区别
Auto. JS learning notes 5:ui interface foundation of autojs Chapter 1
redis学习记录:字典(dict)源码分析
Propagation of transactions
How PLC constructs shift function block (FC) by itself
Spark complex structure data retrieval method
Oculus quest generation opens Bluetooth connection
JVM (VI) Virtual machine bytecode execution engine (with stack execution process and bytecode instruction table)
[chromium] location information kernel debugging
Auto. JS learning notes 8: some common and important APIs
PLC如何自行构造移位功能块(FC)
ASP. Net core permission system practice (zero)
Dynamic proxy
Create simple windowing programs using Visual Studio 2017
在线电路仿真以及开源电子硬件设计介绍