当前位置:网站首页>[go] how to control the maximum number of concurrent processes
[go] how to control the maximum number of concurrent processes
2022-07-26 01:16:00 【ZibeSun】
【Go】 How to control the maximum number of concurrent processes
Abstract
This article will explain in Go How to use channel To limit the maximum concurrency .
The author's level is limited , If you have any questions, please leave a message at the bottom of the article !
introduce - Problems encountered during concurrent programming
I met a demand when moving bricks two days ago , It takes a lot of requests for information from another service . So I enabled multiple coroutines to send requests concurrently , After writing the self-test, I had a wave , It's OK to have a small request at first , When more than one request is made , Many requests begin to fail , Report the following error :socket: too many open files.
This error is due to linux Number of files opened by a process in ( Handle ) We've reached the ceiling , That is to say, there are too many concurrent processes .
Let's talk about how to use... In code channel To limit the maximum concurrency .
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:n err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:t err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:v err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:l err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:s err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:h err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:r err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
2022-05-30T15:21:50.624+0800 ERROR project_manage/organization_service.go:156 func:GetProjectSimpleList GetStaffBaseInfoApi engName:g err:Get "http://xxx": dial tcp x.xx.xx.xx:80: socket: too many open files
Use channel Limit concurrency
Use channel The principle of limiting concurrency is very simple , Take advantage of Go in channel When the buffer is full , Will block writing .
The basic logic is as follows :
- Create a
channel, Set the buffer to the maximum number of concurrency you want , such as 100. - Every time a collaboration is created , Just go
channelWrite a value in , Express placeholder . - At the end of a collaboration execution , From
channelRead out a value in , Express Abdication . - When the maximum number of concurrent processes currently executing at the same time reaches , namely
channelThe buffer of is full , At this time, the new cooperation process is to be executed , placeholder Will be blocked , Until another collaboration exits Abdication after , Will execute .
The principle of this method is simple , The code is simple to implement , Very elegant . But here's the thing , When the maximum concurrent number does not touch other limits ( For example, there are too many simultaneous requests , The number of handles opened by the process is limited ), The maximum concurrent number is not more , The faster the program executes , We can write another article to further discuss this issue .
Specifically Go The code is as follows :
func GetInfo(idList []int) error {
var wg sync.WaitGroup
// Limit the maximum number of concurrent to 100
limitGoroutine := make(chan int, 100)
wg.Add(len(idList))
for _, id := range idList {
// Every time you start a collaboration , Just go channel Write a single digit in
limitGoroutine <- 0
go func() {
// Concurrent requests for information
// Store in database
wg.Done()
// When the cooperation process exits after execution , From channel Read one digit in
<-limitGoroutine
}()
}
wg.Wait()
return nil
}
边栏推荐
- Centrosymmetric binary mode cslbp, matlab
- 健身房一年关店8000家,逆势盈利的工作室是怎么开的?
- Small sample learning - getting started
- Oracle - isupplier portal Invoicing error
- [software development specification iv] application system security coding specification
- 如何获取广告服务流量变现数据,助力广告效果分析?
- Redis数据结构详解,结合书本
- Openvino installation pit notes
- Tutorial on principles and applications of database system (053) -- MySQL query (XV): usage of character functions
- 数据库系统原理与应用教程(054)—— MySQL 查询(十六):日期时间型函数的用法
猜你喜欢

Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock

网络性能评估工具 ping/mtr

What are the ways to quickly improve programming skills in the process of programming learning?

【秒杀概念】大小端

代理IP服务器如何保证自身在网络中的信息安全呢

ZK-Rollups工作原理

How can I become an irreplaceable programmer?

Sqli-labs Less7
![[laser principle and application -3]: foreign brands of lasers](/img/8a/620544bb52c6b8c4db83f9a14783bb.png)
[laser principle and application -3]: foreign brands of lasers

Embedded development: tips and tricks -- seven tips for designing powerful boot loader
随机推荐
Codeforces Round #810 (Div. 2)A~C
[laser principle and application-4]: internal structure and working principle of laser
Docker高级篇-Mysql主从复制
How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP
The application and principle of changing IP software are very wide. Four methods of dynamic IP replacement are used to protect network privacy
力扣 25. K 个一组翻转链表
嵌入式开发:技巧和窍门——设计强大的引导加载程序的7个技巧
The difference and application of in and exists in SQL statement
数据库系统原理与应用教程(057)—— MySQL 练习题
Fundamentals of MATLAB shift operation
Diablo: immortality mobile game how to open more brick moving and novice introduction brick moving strategy
Lua基础语法
Web middleware log analysis script 3.0 (shell script)
Kubernetes Pod启动流程
Arthas watch 命令查看数组中对象的属性
数据库系统原理与应用教程(055)—— MySQL 查询(十七):数学函数的用法
MMOCR使用指南
Gcdqueue encapsulation
Some abnormal error reports and precautions of flowable (1)
Browser development and use skills