当前位置:网站首页>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
}
}
......
边栏推荐
- JVM (III) Virtual machine performance monitoring & fault handling tool
- Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
- one
- postgresql 使用存储过程,拼接多张表,查询数据
- 古董级MFC/GDI+框架LCD显示控件
- Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
- [CEGUI] resource loading process
- 机器学习不是你想用,想用就能用
- 数组,整型,字符变量在全局和局部的存在形式
- CLAHE in opencv for 16 bit image enhancement display
猜你喜欢

Research on autojs wechat: the control IP in wechat on different versions of wechat or simulators is different.
![[experiment] MySQL master-slave replication and read-write separation](/img/aa/7d0799013ff749cacf44ba3b773dff.png)
[experiment] MySQL master-slave replication and read-write separation

Pagoda chevereto1.6.2 the latest version of stepping on the pit tutorial in Chinese

容器江湖的爱恨情仇
![[Wayland] Wayland introduction and customized guidance](/img/4e/655d484ca7787f39aab446fdb1ae74.jpg)
[Wayland] Wayland introduction and customized guidance

ASP.NET Core权限系统实战(零)

远程桌面不能复制粘贴解决办法

MYSQL用户与权限管理,角色管理

2021-03-26

Introduction to IOT
随机推荐
Introduction to IOT
1268_ Implementation of FreeRTOS task context switching
golang中的定时器
Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
spark_ sql
【实验】MySQL主从复制及读写分离
2022淘宝618超级喵运会玩法攻略 618超级喵运会玩法技巧
ASP.NET Core权限系统实战(零)
MYSQL用户与权限管理,角色管理
How Qualcomm platform modifies special voltage
不需要安装防毒软件的系统Chromebook
Circuitbreaker fuse of resilience4j - circuitbreakerevent event
PHP curl function
How PLC constructs shift function block (FC) by itself
PostgreSQL uses stored procedures to splice multiple tables and query data
C break continue return
看看你有没有陷入“标签化”客户和 用户 的陷阱?
High performance computing framework for image processing
Download Notepad++
Periodic pains of cross-border e-commerce? Papaya mobile power as an independent station enabler