当前位置:网站首页>Timers in golang
Timers in golang
2022-06-12 10:23:00 【love666666shen】
stay golang In the program , have access to crontab Time task processing , It can also be done through Timer or Ticker Simply set the scheduled task .
Timer and Ticker The difference between timers is :
Use Timer When timer , Need to call Reset Method to reset the timer event ; and Ticker Timing does not need to call Reset.
Timer Examples of timed tasks :
func process() {
interval := 6 * time.Second
timer := time.NewTimer(interval)
ctx, _ := context.WithTimeout(ctx, 2 * time.Second)
// Timed task processing logic
processLogic()
for {
// call Reset Method pair timer Object to reset the timer
timer.Reset(interval)
select {
case <-ctx.Done():
// timeout handler
log.Info("process timeout, exit")
return
case <-timer.C:
// Timed task processing logic
processLogic()
}
}
}
Ticker Timing task , Unwanted Reset Reset timer .Ticker Examples of scheduled tasks are as follows :
......
ticker := time.NewTicker(time.Second)
ctx, _ := context.WithTimeout(ctx, 2 * time.Second)
for {
select {
case <-ticker.C:
// Timed task processing logic
...
case <-ctx.Done():
fmt.Println("request timeout")
// End the scheduled task
return
}
}
......
边栏推荐
- 机器学习不是你想用,想用就能用
- 2021-02-21
- Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
- Spark complex structure data retrieval method
- reflex
- [DDS] ddsi-rtps specification
- [CEGUI] font resource loading process
- 在App内跳转微信,打开微信
- 3. Abstract Factory
- How PLC constructs shift function block (FC) by itself
猜你喜欢

3. Abstract Factory

Qt自定义窗口圆角

一文读懂Dfinity生态中的首个NFT平台:IMPOSSIBLE THINGS

MySQL user and permission management, role management
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000

机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】

Basic use of scratch
![[experiment] MySQL master-slave replication and read-write separation](/img/aa/7d0799013ff749cacf44ba3b773dff.png)
[experiment] MySQL master-slave replication and read-write separation

容器江湖的爱恨情仇

Auto. JS debugging: use the network mode of lightning simulator for debugging
随机推荐
Circuitbreaker fuse of resilience4j - circuitbreakerregistry register
np. Meshgrid() function and coordinate position generation in 3D space and numpy Introduction to repeat() function
【MySQL】sql_ Model mode
Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
[CEGUI] resource loading process
Docker compose integrates redis, MySQL and microservices, and services are containerized
[Wayland] Weston startup process analysis
[untitled]
The difference between static method locking and non static method locking
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
Yarn scheduling
JVM (VIII) Thread safety and lock optimization
[CEGUI] log system
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
PHP generate schedule
High performance computing framework for image processing
MySQL user and permission management, role management
PHP maximum balance method to solve the problem that the sum of the final percentages is not equal to 100
2021-02-12
Jump to wechat in app and open wechat