脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Golang - 提升应用性能:Go中的同步与异步处理

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

2023-11-06 11:22爱发白日梦的后端 Golang

在异步处理方式中,任务独立并同时执行。这意味着程序在一个任务完成之前不会等待它继续下一个任务。在Golang中,可以使用Goroutines和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请求,比较两种方式的性能。

原文地址:https://mp.weixin.qq.com/s/C1AZ3NX36C-T32wNoZ0kKw

延伸 · 阅读

精彩推荐
  • Golang深入解析golang bufio

    深入解析golang bufio

    这篇文章主要介绍了golang bufio解析,golang的bufio库使用缓存来一次性进行大块数据的读写,以此降低IO系统调用,提升性能,需要的朋友可以参考下...

    charlieroro10232022-09-26
  • Golang详解Go语言的计时器

    详解Go语言的计时器

    Go语言的标准库里提供两种类型的计时器Timer和Ticker。这篇文章主要介绍了Go语言的计时器的相关知识,需要的朋友可以参考下 ...

    Kevin3252020-07-06
  • Golanggolang 中 channel 的详细使用、使用注意事项及死锁问题解析

    golang 中 channel 的详细使用、使用注意事项及死锁问题解析

    这篇文章主要介绍了golang 中 channel 的详细使用、使用注意事项及死锁分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需...

    九卷技术录9272022-09-07
  • Golang深入了解Golang官方container/heap用法

    深入了解Golang官方container/heap用法

    在 Golang 的标准库 container 中,包含了几种常见的数据结构的实现,其实是非常好的学习材料。今天我们就来看看 container/heap 的源码,了解一下官方的同学...

    ag99207502022-11-25
  • GolangGolang你一定要懂的连接池实现

    Golang你一定要懂的连接池实现

    这篇文章主要介绍了Golang你一定要懂的连接池实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    源代码12382020-08-25
  • Golanggo语言中的interface使用实例

    go语言中的interface使用实例

    这篇文章主要介绍了go语言中的interface使用实例,go语言中的interface是一组未实现的方法的集合,如果某个对象实现了接口中的所有方法,那么此对象就实现了此...

    Golang教程网5282020-04-26
  • GolangGo语言中使用flag包对命令行进行参数解析的方法

    Go语言中使用flag包对命令行进行参数解析的方法

    这篇文章主要介绍了Go语言中使用flag包对命令行进行参数解析的方法,文中举了一个实现flag.Value接口来自定义flag的例子,需要的朋友可以参考下 ...

    leanote6312020-04-29
  • Golanggolang 一次性定时器Timer用法及实现原理详解

    golang 一次性定时器Timer用法及实现原理详解

    这篇文章主要为大家介绍了golang 一次性定时器Timer用法及实现原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加...

    yi个俗人11942022-11-12