猎趣网站怎样做网站推广啊
猎趣网站,怎样做网站推广啊,营销手段有哪些方式,百度指数数据分析同步处理
在同步处理方式中#xff0c;任务按顺序一个接一个地执行。每个任务必须在下一个任务开始之前完成。这意味着如果某个任务需要花费大量时间来完成#xff0c;它可能会阻塞后续任务的执行#xff0c;导致潜在的性能瓶颈。 一个简单的现实生活中的例子是两个人在喝啤…同步处理
在同步处理方式中任务按顺序一个接一个地执行。每个任务必须在下一个任务开始之前完成。这意味着如果某个任务需要花费大量时间来完成它可能会阻塞后续任务的执行导致潜在的性能瓶颈。 一个简单的现实生活中的例子是两个人在喝啤酒时进行对话。一个人说一些话并提问另一个人根据情况回应然后反过来… 在下面的示例中每个URL调用必须完成其整个请求-响应周期并提供响应或错误以便可以进行后续的URL调用。
package mainimport (fmtnet/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 (fmtnet/httpsync
)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 urls 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.mzph.cn/bicheng/87260.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!