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

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


在开发过程中,当需要同时处理多个操作时,开发者经常面临同步和异步两种处理方式的选择。


同步处理


在同步处理方式中,任务按顺序一个接一个地执行。每个任务必须在下一个任务开始之前完成。这意味着如果某个任务需要花费大量时间来完成,它可能会阻塞后续任务的执行,导致潜在的性能瓶颈。


一个简单的现实生活中的例子是两个人在喝啤酒时进行对话。一个人说一些话并提问,另一个人根据情况回应,然后反过来...


在下面的示例中,每个URL调用必须完成其整个请求-响应周期并提供响应或错误,以便可以进行后续的URL调用。


package main
import (
 "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 urls
 urlSlice := []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.com
End of sync processing !!


异步处理


在异步处理方式中,任务独立并同时执行。这意味着程序在一个任务完成之前不会等待它继续下一个任务。在Golang中,可以使用Goroutines和Go运行时来实现异步编程。


一个简单的现实生活中的例子是去汽车修理店。一旦工程师处理完其他任务,他们会处理你的任务。在此期间,你可以做其他重要的事情,直到你的汽车被取走并修好。


在下面的示例中,每个URL调用将通过goroutine在自己的线程中进行,并根据需要处理响应。


package main
import (
 "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 urls
 urlSlice := []string{
  "https://www.baidu.com",
  "https://www.csdn.net",
  "https://www.runoob.com",
 }
 var wg sync.WaitGroup
 for _, u := range urlSlice {
  wg.Add(1)
  //all the url's to get error/response are called in their own separate thread via goroutines
  go 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.net
End of sync processing !!


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

相关文章
|
1月前
|
Go 数据库 UED
[go 面试] 同步与异步:程序执行方式的不同之处
[go 面试] 同步与异步:程序执行方式的不同之处
|
关系型数据库 MySQL Go
使用go-mysql-elasticsearch同步mysql数据库信息到ElasticSearch
本文介绍如何使用go-mysql-elasticsearch同步mysql数据库信息到ElasticSearch。1.go-mysql-elasticsearch简介go-mysql-elasticsearch是一个将MySQL数据自动同步到Elasticsearch的服务。
4293 0
|
4月前
|
消息中间件 Go
Go语言高级并发编程技术:深入探索同步原语与复杂并发模式
【2月更文挑战第6天】在Go语言中,除了基础的goroutine和channel之外,还提供了丰富的同步原语和高级并发模式。本文将深入探讨Go语言中的sync包、atomic包以及更复杂的并发模式,如管道、消息队列和生产者消费者模型。通过理解这些高级并发编程技术,我们可以更好地构建高性能、可扩展的并发系统。
|
4月前
|
安全 Go
无缓冲通道:Go语言同步之道
无缓冲通道:Go语言同步之道
59 0
|
NoSQL Go Redis
Asynq: 基于Redis实现的Go生态分布式任务队列和异步处理库
Asynq: 基于Redis实现的Go生态分布式任务队列和异步处理库
446 0
|
存储 安全 Go
Go中sync.WaitGroup处理协程同步
Go中sync.WaitGroup处理协程同步
100 0
|
Go 调度 开发者
并发与并行,同步和异步,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang并发编程之GoroutineEP13
如果说Go lang是静态语言中的皇冠,那么,Goroutine就是并发编程方式中的钻石。Goroutine是Go语言设计体系中最核心的精华,它非常轻量,一个 Goroutine 只占几 KB,并且这几 KB 就足够 Goroutine 运行完,这就能在有限的内存空间内支持大量 Goroutine协程任务,方寸之间,运筹帷幄,用极少的成本获取最高的效率,支持了更多的并发,毫无疑问,Goroutine是比Python的协程原理事件循环更高级的并发异步编程方式。
并发与并行,同步和异步,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang并发编程之GoroutineEP13
|
Go
Go并发之同步异步、异步回调
众所周知,Go语言最强大的地方在于它支持的高并发特性。下面我们先来了解一下Go并发的一些理论基础:同步异步、异步回调。也顺带在此介绍一下进程、线程、协程的区别。
650 0
Go并发之同步异步、异步回调
|
Java Go
Go 语言传统的同步 goroutine 的机制
在这方面,JAVA确实细致, 至于GO能否在线程池方法有JAVA的优势, 要时间证明。
1213 0
|
Go 编译器
go的同步模型
首先来看一段代码,这是The Go Memory Model一文中的一个例子   var a, b int   func f() {     a = 1     b = 2 } func g() {     print(b)     print(a) } ...
706 0