当前位置:网站首页>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 .
边栏推荐
- Entity of cesium data visualization (Part 1)
- Information Security Experiment 3: the use of PGP email encryption software
- JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
- Jenkins task grouping
- NATAPP内网穿透
- 第一讲:包含min函数的栈
- 4、 Fundamentals of machine learning
- Jmeters use
- flex弹性布局
- Some pit avoidance guidelines for using Huawei ECS
猜你喜欢
随机推荐
stm32和电机开发(从单机版到网络化)
二叉树高频题型
Selenium mouse sliding operation event
Mysql数据库-锁-学习笔记
Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
第一讲:包含min函数的栈
asp. How to call vb DLL function in net project
Locust performance test 4 (custom load Policy)
nlohmann json
C language pointer (Part 2)
(3/8)枚举的不当用法 之 方法参数(二)
其实特简单,教你轻松实现酷炫的数据可视化大屏
Postman interface test (I. installation and use)
Unity shader (learn more about vertex fragment shaders)
Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
Binary tree high frequency question type
DRF authentication, permissions, and flow restrictions (only for views in DRF)
Mysql database index study notes
第一讲:鸡蛋的硬度