当前位置:网站首页>How to solve the problem of golang select mechanism and timeout
How to solve the problem of golang select mechanism and timeout
2022-07-07 09:29:00 【Yisu cloud】
golang select How to solve the mechanism and timeout problems
This article mainly introduces golang select Mechanism and how to solve the timeout problem , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article golang select How to solve the problem of mechanism and timeout will be fruitful , Let's have a look .
golang The coroutine in is very convenient to use , But when the collaboration process ends is a control problem , It can be used select In combination with .
The communication between the child and parent processes usually uses context perhaps chan. I came across a common usage scenario , Try to process more than once in the subprocess , The parent process timed out waiting for a period of time , I choose to use chan Realization . I thought select and C++ in switch similar , So the initial code looks like this :
for { select { case <-ctx.Done(): // process ctx done case <-time.After(time.Second * 3): // process after default: // process code }}
The test found that... Could not be realized timeout, Check the document carefully , Only then discovered golang in select There's another mystery . Don't talk nonsense , Summarize the main points directly :
select Medium case Must be carried out chan Manipulation of , So it can only be in case In the operation chan, And is Non blocking reception .
select Medium case Monitoring at the same time , Multiple case Simultaneous operation , Not switch Judge one by one . If more than one case Meet the requirements , Execute a random , If one does not exist, the current coroutine will be blocked ( No, default Under the circumstances ). Is very similar Linux File symbol operation select semantics .
There is no blockage mentioned above default Under the circumstances , If there is default, execute default, And then quit select, That is, the current process will not be blocked .
Go back to the above code , This is me. select Will continue to implement default, time.After
Generated chan Will not be blocked to judge , So I can't achieve the effect I want . After understanding, modify the code again :
done := make(char int)go func(c chan int) { for { // process code if { c <- 1 return } } c <- 0}(done)select { case <-ctx.Done(): // process ctx done case <-time.After(time.Second * 3): // process after case <-done: // process code}
Open a new process to try again and again , Three outside case There is a satisfaction , Will perform . But there is a problem that needs attention : When does the subprocess exit ? .
because gorountine Can't be forced kill, So in the case of the above timeout ,select Statement execution case time.After
Then quit , done
This chan There is no receiver , So there is no receiver , There is no buffer , combination chan Characteristics of , The subprocess will always be blocked and cannot exit , So in essence, this implementation will lead to the accumulation of sub processes , That is to say Xiecheng leakage , May run out of resources .
How to avoid the above problems ? A very simple idea is to provide buffers , done := make(char int, 1)
, So even if there is no receiver , The subprocess can also be sent , Not blocked .
There's another way , It says ,select operation chan, And you can specify default, Is there a way of thinking ?
if { select { case done <- 1: default: return }}
About “golang select How to solve the mechanism and timeout problems ” That's all for this article , Thank you for reading ! I'm sure you're right “golang select How to solve the mechanism and timeout problems ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .
边栏推荐
- Port multiplexing and re imaging
- Where is the answer? action config/Interceptor/class/servlet
- Jemter operation
- (3/8) method parameters of improper use of enumeration (2)
- C language pointer (special article)
- Netease cloud wechat applet
- Mysql数据库-锁-学习笔记
- NETCORE 3.1 solves cross domain problems
- Loxodonframework quick start
- 创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
猜你喜欢
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
四、机器学习基础
信息安全实验二 :使用X-SCANNER扫描工具
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
其实特简单,教你轻松实现酷炫的数据可视化大屏
Locust performance test 5 (analysis)
Storage of data in memory
Unity shader (to achieve a simple material effect with adjustable color attributes only)
STM32 and motor development (from stand-alone version to Networking)
第一讲:寻找矩阵的极小值
随机推荐
Jenkins+ant+jmeter use
其实特简单,教你轻松实现酷炫的数据可视化大屏
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
NETCORE 3.1 solves cross domain problems
asp. How to call vb DLL function in net project
Mysql database index study notes
二叉树高频题型
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
[cloud native] Devops (I): introduction to Devops and use of code tool
Upload taro pictures to Base64
ComputeShader
【SVN】SVN是什么?怎么使用?
CMD startup software passes in parameters with spaces
Jmeters use
软件建模与分析
Pycharm create a new file and add author information
Jenkins modifies the system time
进程和线程的区别
沙龙预告|GameFi 领域的瓶颈和解决方案
Locust performance test 2 (interface request)