当前位置:网站首页>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 .
边栏推荐
- Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute
- 其实特简单,教你轻松实现酷炫的数据可视化大屏
- golang select机制和超时问题怎么解决
- Postman setting environment variables
- Self awakening from a 30-year-old female programmer
- Register address name mapping
- Netease cloud wechat applet
- C language pointer (Part 2)
- Leetcode question brushing record (array) combination sum, combination sum II
- Netease Cloud Wechat applet
猜你喜欢

Error: selenium common. exceptions. WebDriverException: Messag‘geckodriver‘ execute

NATAPP内网穿透

Information Security Experiment 2: using x-scanner scanning tool

esp8266使用TF卡并读写数据(基于arduino)

Postman interface test (II. Set global variables \ sets)

NVIC interrupt priority management

Mysql database transaction learning notes

第一讲:包含min函数的栈

Mysql database lock learning notes

其实特简单,教你轻松实现酷炫的数据可视化大屏
随机推荐
Binary tree high frequency question type
第一讲:包含min函数的栈
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
MySQL common statements
Network request process
CMD startup software passes in parameters with spaces
Detailed learning notes of JVM memory structure (I)
ComputeShader
Postman interface debugging method
Jmeters use
Leetcode question brushing record (array) combination sum, combination sum II
H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
Mysql数据库-锁-学习笔记
STM32 and motor development (from stand-alone version to Networking)
C language pointer (special article)
Postman data driven
Confitest of fixture py
[cloud native] Devops (I): introduction to Devops and use of code tool
第一讲:寻找矩阵的极小值
Where is the answer? action config/Interceptor/class/servlet