当前位置: 首页 > news >正文

提升应用性能:Go中的同步与异步处理

同步处理

在同步处理方式中,任务按顺序一个接一个地执行。每个任务必须在下一个任务开始之前完成。这意味着如果某个任务需要花费大量时间来完成,它可能会阻塞后续任务的执行,导致潜在的性能瓶颈。
一个简单的现实生活中的例子是两个人在喝啤酒时进行对话。一个人说一些话并提问,另一个人根据情况回应,然后反过来…
在下面的示例中,每个URL调用必须完成其整个请求-响应周期并提供响应或错误,以便可以进行后续的URL调用。

package mainimport ("fmt""net/http"
)func makeUrlCall(url string) {_, err := http.Get(url)if err != nil {fmt.Println("Got error in connecting to url: ", url)}fmt.Println("Got the response from our url: ", url)
}func main() {fmt.Println("Welcome here !!")fmt.Println()//slice of urlsurlSlice := []string{"https://www.baidu.com","https://www.csdn.net","https://www.runoob.com",}for idx, url := range urlSlice {fmt.Println("Calling url on index: ", idx)makeUrlCall(url)}fmt.Println()fmt.Println("End of sync processing !!")return
}

输出:

Welcome here !!Calling url on index:  0
Got the response from our url:  https://www.baidu.com
Calling url on index:  1
Got the response from our url:  https://www.csdn.net
Calling url on index:  2
Got the response from our url:  https://www.runoob.comEnd of sync processing !!

异步处理

在异步处理方式中,任务独立并同时执行。这意味着程序在一个任务完成之前不会等待它继续下一个任务。在Golang中,可以使用Goroutines和Go运行时来实现异步编程。
一个简单的现实生活中的例子是去汽车修理店。一旦工程师处理完其他任务,他们会处理你的任务。在此期间,你可以做其他重要的事情,直到你的汽车被取走并修好。
在下面的示例中,每个URL调用将通过goroutine在自己的线程中进行,并根据需要处理响应。

package mainimport ("fmt""net/http""sync"
)func makeUrlCall(url string) {_, err := http.Get(url)if err != nil {fmt.Println("Got error in connecting to url: ", url)}fmt.Println("Got the response from our url: ", url)
}func main() {fmt.Println("Welcome here !!")fmt.Println()//slice of urlsurlSlice := []string{"https://www.baidu.com","https://www.csdn.net","https://www.runoob.com",}var wg sync.WaitGroupfor _, u := range urlSlice {wg.Add(1)//all the url's to get error/response are called in their own separate thread via goroutinesgo func(url string) {defer wg.Done()makeUrlCall(url)}(u)}wg.Wait()fmt.Println()fmt.Println("End of sync processing !!")return
}

输出:

Welcome here !!Got the response from our url:  https://www.baidu.com
Got the response from our url:  https://www.runoob.com
Got the response from our url:  https://www.csdn.netEnd of sync processing !!

如果我们在切片中添加更多的URL并进行更多的HTTP get请求,比较两种方式的性能。

http://www.dtcms.com/a/486626.html

相关文章:

  • OpenAI 最新开源模型 gpt-oss 架构与训练解析
  • 网站片区韶关网站seo
  • 红帽Linux复习-从命令行管理文件、创建查看编辑文本文件
  • C++11(lambda和包装器)
  • 时钟特性约束(四)
  • 免费公司网站如何建立设计品牌网页设计
  • jQuery 尺寸
  • 做木工网站天河建设网站公司排名
  • 嵌入式软件--STM32 SPI通信(上)
  • PHP “真异步“ TrueAsync SAPI 与 NGINX Unit 集成
  • ARCGIS国土超级工具集1.7更新说明
  • 网站如何做信息表美图在线图片编辑器
  • 分布式专题——45 ElasticSearch基础数据管理详解
  • SQLBot:基于大模型与 RAG 的智能问数系统架构
  • 网站系统维护要多久福清市建设工程交易网站好像
  • 在Linux系统上执行 setenv CDS_XVNC_TENBASE + 后重启Virtuoso的作用
  • SpringBoot用户登录注册系统设计与实现
  • 杭州余杭做网站公司秦皇岛 网站建设
  • 并查集-990.等式方程的可满足性-力扣(LeetCode)
  • Jmeter是如何实现接口关联的?
  • 企业网站做seo的必要性关于网站开发人员的薪资
  • 【Qt】6.Qt对象树
  • 使用Scanpy的基本操作
  • openssl s_client 命令——测试、调试和诊断 SSL/TLS 连接
  • 深度学习中的RNN与LSTM:原理、差异与应用
  • 湖南智能网站建设报价广西南宁网站排名优化
  • 广州网站建设十年乐云seo北京东直门网站建设
  • h5游戏免费下载:卡通飞行
  • IntelliJ IDEA 配置 Gitee 私人令牌完整指南(2025 年最新版)
  • 智尚招聘求职小程序v1.0.23