上回在 用 Go 写一个轻量级的 ssh 批量操做工具 里说起过,咱们作 Golang 并发的时候要对并发进行限制,对 goroutine 的执行要有超时控制。那会没有细说,这里展开讨论一下。golang
如下示例代码所有能够直接在 The Go Playground 上运行测试:数组
咱们先来跑一个简单的并发看看服务器
package main import ( "fmt" "time" ) func run(task_id, sleeptime int, ch chan string) { time.Sleep(time.Duration(sleeptime) * time.Second) ch <- fmt.Sprintf("task id %d , sleep %d second", task_id, sleeptime) return } func main() { input := []int{3, 2, 1} ch := make(chan string) startTime := time.Now() fmt.Println("Multirun start") for i, sleeptime := range input { go run(i, sleeptime, ch) } for range input { fmt.Println(<-ch) } endTime := time.Now() fmt.Printf("Multissh finished. Process time %s. Number of tasks is %d", endTime.Sub(startTime), len(input)) }
函数 run()
接受输入的参数,sleep
若干秒。而后经过 go
关键字并发执行,经过 channel
返回结果。并发
channel
顾名思义,他就是 goroutine
之间通讯的“管道"。管道中的数据流通,其实是 goroutine
之间的一种内存共享。咱们经过他能够在 goroutine
之间交互数据。ssh
ch <- xxx // 向 channel 写入数据 <- ch // 从 channel 中读取数据
channel
分为无缓冲(unbuffered)和缓冲(buffered)两种。例如刚才咱们经过以下方式建立了一个无缓冲的 channel
。函数
ch := make(chan string)
channel
的缓冲,咱们一会再说,先看看刚才看看执行的结果。工具
Multirun start task id 2 , sleep 1 second task id 1 , sleep 2 second task id 0 , sleep 3 second Multissh finished. Process time 3s. Number of tasks is 3 Program exited.
三个 goroutine
`分别 sleep 了 3,2,1秒。但总耗时只有 3 秒。因此并发生效了,go 的并发就是这么简单。测试
刚才的示例中,我执行任务的顺序是 0,1,2。可是从 channel
中返回的顺序倒是 2,1,0。这很好理解,由于 task 2 执行的最快嘛,因此先返回了进入了 channel
,task 1 次之,task 0 最慢。spa
若是咱们但愿按照任务执行的顺序依次返回数据呢?能够经过一个 channel
数组(好吧,应该叫切片)来作,好比这样code
package main import ( "fmt" "time" ) func run(task_id, sleeptime int, ch chan string) { time.Sleep(time.Duration(sleeptime) * time.Second) ch <- fmt.Sprintf("task id %d , sleep %d second", task_id, sleeptime) return } func main() { input := []int{3, 2, 1} chs := make([]chan string, len(input)) startTime := time.Now() fmt.Println("Multirun start") for i, sleeptime := range input { chs[i] = make(chan string) go run(i, sleeptime, chs[i]) } for _, ch := range chs { fmt.Println(<-ch) } endTime := time.Now() fmt.Printf("Multissh finished. Process time %s. Number of tasks is %d", endTime.Sub(startTime), len(input)) }
运行结果,如今输出的次序和输入的次序一致了。
Multirun start task id 0 , sleep 3 second task id 1 , sleep 2 second task id 2 , sleep 1 second Multissh finished. Process time 3s. Number of tasks is 3 Program exited.
刚才的例子里咱们没有考虑超时。然而若是某个 goroutine
运行时间太长了,那很确定会拖累主 goroutine
被阻塞住,整个程序就挂起在那儿了。所以咱们须要有超时的控制。
一般咱们能够经过select
+ time.After
来进行超时检查,例如这样,咱们增长一个函数 Run()
,在 Run()
中执行 go run()
。并经过 select
+ time.After
进行超时判断。
package main import ( "fmt" "time" ) func Run(task_id, sleeptime, timeout int, ch chan string) { ch_run := make(chan string) go run(task_id, sleeptime, ch_run) select { case re := <-ch_run: ch <- re case <-time.After(time.Duration(timeout) * time.Second): re := fmt.Sprintf("task id %d , timeout", task_id) ch <- re } } func run(task_id, sleeptime int, ch chan string) { time.Sleep(time.Duration(sleeptime) * time.Second) ch <- fmt.Sprintf("task id %d , sleep %d second", task_id, sleeptime) return } func main() { input := []int{3, 2, 1} timeout := 2 chs := make([]chan string, len(input)) startTime := time.Now() fmt.Println("Multirun start") for i, sleeptime := range input { chs[i] = make(chan string) go Run(i, sleeptime, timeout, chs[i]) } for _, ch := range chs { fmt.Println(<-ch) } endTime := time.Now() fmt.Printf("Multissh finished. Process time %s. Number of task is %d", endTime.Sub(startTime), len(input)) }
运行结果,task 0 和 task 1 已然超时
Multirun start task id 0 , timeout task id 1 , timeout tasi id 2 , sleep 1 second Multissh finished. Process time 2s. Number of task is 3 Program exited.
若是任务数量太多,不加以限制的并发开启 goroutine
的话,可能会过多的占用资源,服务器可能会爆炸。因此实际环境中并发限制也是必定要作的。
一种常见的作法就是利用 channel
的缓冲机制——开始的时候咱们提到过的那个。
咱们分别建立一个带缓冲和不带缓冲的 channel
看看
ch := make(chan string) // 这是一个无缓冲的 channel,或者说缓冲区长度是 0 ch := make(chan string, 1) // 这是一个带缓冲的 channel, 缓冲区长度是 1
这二者的区别在于,若是 channel
没有缓冲,或者缓冲区满了。goroutine
会自动阻塞,直到 channel
里的数据被读走为止。举个例子
package main import ( "fmt" ) func main() { ch := make(chan string) ch <- "123" fmt.Println(<-ch) }
这段代码执行将报错
fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan send]: main.main() /tmp/sandbox531498664/main.go:9 +0x60 Program exited.
这是由于咱们建立的 ch
是一个无缓冲的 channel
。所以在执行到 ch<-"123"
,这个 goroutine
就阻塞了,后面的 fmt.Println(<-ch)
没有办法获得执行。因此将会报 deadlock
错误。
若是咱们改为这样,程序就能够执行
package main import ( "fmt" ) func main() { ch := make(chan string, 1) ch <- "123" fmt.Println(<-ch) }
执行
123 Program exited.
若是咱们改为这样
package main import ( "fmt" ) func main() { ch := make(chan string, 1) ch <- "123" ch <- "123" fmt.Println(<-ch) fmt.Println(<-ch) }
尽管读取了两次 channel,可是程序仍是会死锁,由于缓冲区满了,goroutine
阻塞挂起。第二个 ch<- "123"
是没有办法写入的。
fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan send]: main.main() /tmp/sandbox642690323/main.go:10 +0x80 Program exited.
所以,利用 channel 的缓冲设定,咱们就能够来实现并发的限制。咱们只要在执行并发的同时,往一个带有缓冲的 channel
里写入点东西(随便写啥,内容不重要)。让并发的 goroutine
在执行完成后把这个 channel
里的东西给读走。这样整个并发的数量就讲控制在这个 channel
的缓冲区大小上。
好比咱们能够用一个 bool
类型的带缓冲 channel
做为并发限制的计数器。
chLimit := make(chan bool, 1)
而后在并发执行的地方,每建立一个新的 goroutine,都往 chLimit
里塞个东西。
for i, sleeptime := range input { chs[i] = make(chan string, 1) chLimit <- true go limitFunc(chLimit, chs[i], i, sleeptime, timeout) }
这里经过 go
关键字并发执行的是新构造的函数。他在执行完原来的 Run()
后,会把 chLimit
的缓冲区里给消费掉一个。
limitFunc := func(chLimit chan bool, ch chan string, task_id, sleeptime, timeout int) { Run(task_id, sleeptime, timeout, ch) <-chLimit }
这样一来,当建立的 goroutine
数量到达 chLimit
的缓冲区上限后。主 goroutine
就挂起阻塞了,直到这些 goroutine
执行完毕,消费掉了 chLimit
缓冲区中的数据,程序才会继续建立新的 goroutine
。咱们并发数量限制的目的也就达到了。
如下是完整代码
package main import ( "fmt" "time" ) func Run(task_id, sleeptime, timeout int, ch chan string) { ch_run := make(chan string) go run(task_id, sleeptime, ch_run) select { case re := <-ch_run: ch <- re case <-time.After(time.Duration(timeout) * time.Second): re := fmt.Sprintf("task id %d , timeout", task_id) ch <- re } } func run(task_id, sleeptime int, ch chan string) { time.Sleep(time.Duration(sleeptime) * time.Second) ch <- fmt.Sprintf("task id %d , sleep %d second", task_id, sleeptime) return } func main() { input := []int{3, 2, 1} timeout := 2 chLimit := make(chan bool, 1) chs := make([]chan string, len(input)) limitFunc := func(chLimit chan bool, ch chan string, task_id, sleeptime, timeout int) { Run(task_id, sleeptime, timeout, ch) <-chLimit } startTime := time.Now() fmt.Println("Multirun start") for i, sleeptime := range input { chs[i] = make(chan string, 1) chLimit <- true go limitFunc(chLimit, chs[i], i, sleeptime, timeout) } for _, ch := range chs { fmt.Println(<-ch) } endTime := time.Now() fmt.Printf("Multissh finished. Process time %s. Number of task is %d", endTime.Sub(startTime), len(input)) }
运行结果
Multirun start task id 0 , timeout task id 1 , timeout task id 2 , sleep 1 second Multissh finished. Process time 5s. Number of task is 3 Program exited.
chLimit
的缓冲是 1。task 0 和 task 1 耗时 2 秒超时。task 2 耗时 1 秒。总耗时 5 秒。并发限制生效了。
若是咱们修改并发限制为 2
chLimit := make(chan bool, 2)
运行结果
Multirun start task id 0 , timeout task id 1 , timeout task id 2 , sleep 1 second Multissh finished. Process time 3s. Number of task is 3 Program exited.
task 0 , task 1 并发执行,耗时 2秒。task 2 耗时 1秒。总耗时 3 秒。符合预期。
有没有注意到代码里有个地方和以前不一样。这里,用了一个带缓冲的 channel
chs[i] = make(chan string, 1)
还记得上面的例子么。若是 channel
不带缓冲,那么直到他被消费掉以前,这个 goroutine
都会被阻塞挂起。
然而若是这里的并发限制,也就是 chLimit
生效阻塞了主 goroutine
,那么后面消费这些数据的代码并不会执行到。。。因而就 deadlock
拉!
for _, ch := range chs { fmt.Println(<-ch) }
因此给他一个缓冲就行了。
从Deadlock报错理解Go channel机制(一)
golang-what-is-channel-buffer-size
golang-using-timeouts-with-channels