当前位置:网站首页>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 .
边栏推荐
- Mysql:select ... for update
- Unity shader (data type in cghlsl)
- Binary tree high frequency question type
- Register address name mapping
- Serializer & modelserializer of DRF serialization and deserialization
- 消费互联网的产业链其实是很短的,它仅仅承接平台上下游的对接和撮合的角色
- VSCode+mingw64
- Netease Cloud Wechat applet
- MySql数据库-事务-学习笔记
- Jenkins+ant+jmeter use
猜你喜欢
Data association between two interfaces of postman
12、 Sort
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
浏览器中如何让视频倍速播放
十二、排序
C language pointer (exercises)
Difference between interface iterator and iteratable
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
Storage of data in memory
Mysql:select ... for update
随机推荐
Jenkins modifies the system time
Oracle安装增强功能出错
Schema-validation: wrong column type encountered in column XXX in table XXX
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
Difference between interface iterator and iteratable
如何使用clipboard.js库实现复制剪切功能
Unity uses mesh to realize real-time point cloud (II)
信息安全实验三 :PGP邮件加密软件的使用
Loxodonframework quick start
JS inheritance prototype
Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
第一讲:包含min函数的栈
華為HCIP-DATACOM-Core_03day
Unittest simple project
**grafana安装**
信息安全实验四:Ip包监视程序实现
Idea development environment installation
Huawei HCIP - datacom - Core 03 jours
scrapy爬虫mysql,Django等
Colorbar of using vertexehelper to customize controls (II)