Go语言实现超时的三种方法实例

作者:湾区的候鸟 时间:2023-06-22 18:32:43 

前言

超时,指一个协程A开启另一个协程B,A会阻塞等待B一段指定的时间,例如:5秒,A通知B结束(也有可能不通知,让B继续运行)。也就是说,A就不愿意阻塞等待太久。

Go语言有多种方法实现这种超时,我总结出3种:

方法一:用两个通道 + A协程sleep

一个通道用来传数据,一个用来传停止信号。

package main

import (
"fmt"
"time"
)

// 老师视频里的生产者消费者

func main() {
//知识点: 老师这里用了两个线程,一个用个传数据,一个用来传关闭信号
messages := make(chan int, 10)
done := make(chan bool)

defer close(messages)

// consumer
go func() {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
select {
case <-done:
fmt.Println("child process interrupt...") // 数据还没收完,就被停止了。
return
default:
fmt.Printf("receive message:%d\n", <-messages)
}

}
}()

// producer
for i := 0; i < 10; i++ {
messages <- i
}

// 5秒后主线程关闭done通道
time.Sleep(5 * time.Second)
close(done)
time.Sleep(1 * time.Second)
fmt.Println("main process exit!")
}

程序输出如下:

receive message:0
receive message:1
receive message:2
receive message:3
child process interrupt...
main process exit!

方法二:使用Timer(定时器)

这种方法也方法一类似,只不过是用一个Timer代替通道。

package main

import (
"fmt"
"time"
)

//知识点:
// 1) 多通道
// 2) 定时器
func main() {
ch1 := make(chan int, 10)
go func(ch chan<- int) {
// 假设子协程j是一个耗时操作,例如访问网络,要10秒后才会有数据
time.Sleep(10 * time.Second)
ch <- 1
}(ch1)

timer := time.NewTimer(5 * time.Second) // 设置定时器的超时时间,主线程只等5秒

fmt.Println("select start....")
// 知识点:主协程等待子线程,并有超时机制
select {
case <-ch1:
fmt.Println("从channel 1 收到一个数字")
case <-timer.C: // 定时器也是一个通道
fmt.Println("5秒到了,超时了,main协程不等了")
}

fmt.Println("done!")
}

程序输出如下:

select start....
5秒到了,超时了,main协程不等了
done!

方法三:使用context.WithTimeout

下面的例子比较复杂,基于 Channel 编写一个简单的单协程生产者消费者模型。

要求如下:

1)队列:队列长度 10,队列元素类型为 int

2)生产者:每 1 秒往队列中放入一个类型为 int 的元素,队列满时生产者可以阻塞

3)消费者:每2秒从队列中获取一个元素并打印,队列为空时消费者阻塞

4)主协程30秒后要求所有子协程退出。

5)要求优雅退出,即消费者协程退出前,要先消费完所有的int

6)通过入参支持两种运行模式:

  • wb(温饱模式)生产速度快过消费速度、

  • je(饥饿模式)生产速度慢于消费速度

context.WithTimeout见第87行。

package main

import (
"context"
"flag"
"fmt"
"sync"
"time"
)

// 课后练习 1.2
// 基于 Channel 编写一个简单的单协程生产者消费者模型。
// 要求如下:
// 1)队列:队列长度 10,队列元素类型为 int
// 2)生产者:每 1 秒往队列中放入一个类型为 int 的元素,队列满时生产者可以阻塞
// 3)消费者:每2秒从队列中获取一个元素并打印,队列为空时消费者阻塞
// 4)主协程30秒后要求所有子协程退出。
// 5)要求优雅退出,即消费者协程退出前,要先消费完所有的int。

// 知识点:
// 1) 切片的零值也是可用的。
// 2) context.WithTimeout
var (
wg sync.WaitGroup
p  Producer
c  Consumer
)

type Producer struct {
Time     int
Interval int
}

type Consumer struct {
Producer
}

func (p Producer) produce(queue chan<- int, ctx context.Context) {
go func() {
LOOP:
for {
p.Time = p.Time + 1
queue <- p.Time
fmt.Printf("生产者进行第%d次生产,值:%d\n", p.Time, p.Time)
time.Sleep(time.Duration(p.Interval) * time.Second)

select {
case <-ctx.Done():
close(queue)
break LOOP
}
}
wg.Done()
}()
}

func (c Consumer) consume(queue <-chan int, ctx context.Context) {
go func() {
LOOP:
for {
c.Time++
val := <-queue
fmt.Printf("-->消费者进行第%d次消费,值:%d\n", c.Time, val)
time.Sleep(time.Duration(c.Interval) * time.Second)

select {
case <-ctx.Done():
//remains := new([]int)
//remains := []int{}
var remains []int // 知识点:切片的零值也是可用的。
for val = range queue {
remains = append(remains, val)
fmt.Printf("-->消费者: 最后一次消费, 值为:%v\n", remains)
break LOOP
}
}
}
wg.Done()
}()
}

func main() {
wg.Add(2)

// 知识点:context.Timeout
timeout := 30
ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)

queue := make(chan int, 10)

p.produce(queue, ctx)
fmt.Println("main waiting...")
wg.Wait()
fmt.Println("done")
}

/*
启动命令:
$ go run main/main.go -m wb
$ go run main/main.go -m je
*/
func init() {
// 解析程序入参,运行模式
mode := flag.String("m", "wb", "请输入运行模式:\nwb(温饱模式)生产速度快过消费速度、\nje(饥饿模式)生产速度慢于消费速度)")
flag.Parse()

p = Producer{}
c = Consumer{}

if *mode == "wb" {
fmt.Println("运行模式:wb(温饱模式)生产速度快过消费速度")
p.Interval = 1 // 每隔1秒生产一次
c.Interval = 5 // 每隔5秒消费一次

// p = Producer{Interval: 1}
// c = Consumer{Interval: 5}  // 这一行会报错,为什么?

} else {
fmt.Println("运行模式:je(饥饿模式)生产速度慢于消费速度")
p.Interval = 5 // 每隔5秒生产一次
c.Interval = 1 // 每隔1秒消费一次
}
}

wb(温饱模式)生产速度快过消费速度,输出如下:

运行模式:wb(温饱模式)生产速度快过消费速度
生产者: 第1次生产, 值为:1
-->消费者: 第1次消费, 值为:1
生产者: 第2次生产, 值为:2
生产者: 第3次生产, 值为:3
生产者: 第4次生产, 值为:4
生产者: 第5次生产, 值为:5
-->消费者: 第2次消费, 值为:2
生产者: 第6次生产, 值为:6
生产者: 第7次生产, 值为:7
生产者: 第8次生产, 值为:8
生产者: 第9次生产, 值为:9
生产者: 第10次生产, 值为:10
-->消费者: 第3次消费, 值为:3
生产者: 第11次生产, 值为:11
生产者: 第12次生产, 值为:12
生产者: 第13次生产, 值为:13
-->消费者: 第4次消费, 值为:4
生产者: 第14次生产, 值为:14
-->消费者: 第5次消费, 值为:5
生产者: 第15次生产, 值为:15
生产者: 第16次生产, 值为:16
-->消费者: 第6次消费, 值为:6
main waiting
生产者: 第17次生产, 值为:17
-->消费者: 最后一次消费, 值为:[7 8 9 10 11 12 13 14 15 16 17]
-- done --

je(饥饿模式)生产速度慢于消费速度,输出如下:

运行模式:je(饥饿模式)生产速度慢于消费速度
-->消费者: 第1次消费, 值为:1
生产者: 第1次生产, 值为:1
生产者: 第2次生产, 值为:2
-->消费者: 第2次消费, 值为:2
生产者: 第3次生产, 值为:3
-->消费者: 第3次消费, 值为:3
生产者: 第4次生产, 值为:4
-->消费者: 第4次消费, 值为:4
生产者: 第5次生产, 值为:5
-->消费者: 第5次消费, 值为:5
生产者: 第6次生产, 值为:6
-->消费者: 第6次消费, 值为:6
main waiting
-->消费者: 第7次消费, 值为:0

附:go 实现超时退出

之前手写rpc框架的时候,吃多了网络超时处理的苦,今天偶然发现了实现超时退出的方法,MARK

func AsyncCall() {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*800))
defer cancel()
go func(ctx context.Context) {
// 发送HTTP请求
}()

select {
case <-ctx.Done():
fmt.Println("call successfully!!!")
return
case <-time.After(time.Duration(time.Millisecond * 900)):
fmt.Println("timeout!!!")
return
}
}

//2
func AsyncCall() {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond * 800))
defer cancel()
timer := time.NewTimer(time.Duration(time.Millisecond * 900))

go func(ctx context.Context) {
// 发送HTTP请求
}()

select {
case <-ctx.Done():
timer.Stop()
timer.Reset(time.Second)
fmt.Println("call successfully!!!")
return
case <-timer.C:
fmt.Println("timeout!!!")
return
}
}

//3
func AsyncCall() {
 ctx := context.Background()
done := make(chan struct{}, 1)

go func(ctx context.Context) {
// 发送HTTP请求
done <- struct{}{}
}()

select {
case <-done:
fmt.Println("call successfully!!!")
return
case <-time.After(time.Duration(800 * time.Millisecond)):
fmt.Println("timeout!!!")
return
}
}

来源:https://blog.csdn.net/tjg138/article/details/124114511

标签:go语言,超时
0
投稿

猜你喜欢

  • Python利用pywin32库实现将PPT导出为高清图片

    2023-10-01 22:59:45
  • 在Python中使用matplotlib模块绘制数据图的示例

    2023-08-01 01:39:45
  • 使用python实现飞机大战游戏

    2021-05-11 12:02:16
  • python 简单的股票基金爬虫

    2021-03-13 19:34:46
  • SQL Server存储过程同时返回分页结果集和总数

    2024-01-21 01:31:43
  • 利用pytorch实现对CIFAR-10数据集的分类

    2021-11-21 03:09:36
  • python opencv调用笔记本摄像头

    2022-07-19 14:03:34
  • golang实现unicode转换为字符串string的方法

    2024-05-09 09:30:44
  • python基础之递归函数

    2021-11-29 03:28:30
  • HTML5设计原则

    2012-04-26 16:46:45
  • 用Python在Excel里画出蒙娜丽莎的方法示例

    2023-12-18 02:59:21
  • 解决pycharm中导入自己写的.py函数出错问题

    2023-07-09 12:12:05
  • 使用Pyhton集合set()实现成果查漏的例子

    2023-10-20 17:49:00
  • Python利用DNN实现宝石识别

    2023-08-07 05:49:18
  • Python数据可视化实现多种图例代码详解

    2022-02-11 04:01:38
  • python 获取本机ip地址的两个方法

    2023-04-08 00:17:30
  • python使用imap-tools模块下载邮件附件的示例

    2023-09-16 08:39:38
  • 在vue-cli3中使用axios获取本地json操作

    2023-07-02 17:07:12
  • JS实现普通轮播图特效

    2024-04-17 10:19:52
  • python开启摄像头以及深度学习实现目标检测方法

    2023-10-27 03:23:18
  • asp之家 网络编程 m.aspxhome.com