Files
the-way-to-go_ZH_CN/eBook/18.8.md
songleo 8fdcfd3776 modified: eBook/18.1.md
modified:   eBook/18.10.md
	modified:   eBook/18.11.md
	modified:   eBook/18.2.md
	modified:   eBook/18.3.md
	modified:   eBook/18.4.md
	modified:   eBook/18.5.md
	modified:   eBook/18.6.md
	modified:   eBook/18.7.md
	modified:   eBook/18.8.md
	modified:   eBook/18.9.md
2016-01-03 21:47:46 +08:00

116 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 18.8 协程goroutine与通道channel
出于性能考虑的建议:
实践经验表明,如果你使用并行性获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。
1 出于出于性能考虑建议使用带缓存的通道:
使用带缓存的通道很轻易成倍提高它的吞吐量某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量你可以尝试着更进一步的优化其的性能。
2 限制一个通道的数据数量并将它们封装在成一个数组:
如果使用通道传递大量单独的数据那么通道将变成你的性能瓶颈。然而当将数据块打包封装成数组在接收端解压数据时性能可以提高至10倍。
创建:`ch := make(chan type, buf)`
1如何使用`for`或者`for-range`遍历一个通道:
```go
for v := range ch {
// do something with v
}
```
2如何检测一个通道`ch`是否是关闭的:
```go
//read channel until it closes or error-condition
for {
if input, open := <-ch; !open {
break
}
fmt.Printf(%s , input)
}
```
或者使用1自动检测。
3如何通过一个通道让主程序等待直到协程完成
(信号量模式):
```go
ch := make(chan int) // Allocate a channel.
// Start something in a goroutine; when it completes, signal on the channel.
go func() {
// doSomething
ch <- 1 // Send a signal; value does not matter.
}()
doSomethingElseForAWhile()
<-ch // Wait for goroutine to finish; discard sent value.
```
如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。
4通道的工厂模板下面的函数是一个通道工厂启动一个匿名函数作为协程以生产通道
```go
func pump() chan int {
ch := make(chan int)
go func() {
for i := 0; ; i++ {
ch <- i
}
}()
return ch
}
```
5通道迭代器模板
6如何限制并发处理请求的数量参考[章节14.11](14.11.md)
7如何在多核CPU上实现并行计算参考[章节14.13](14.13.md)
8如何停止一个协程`runtime.Goexit()`
9简单的超时模板
```go
timeout := make(chan bool, 1)
go func() {
time.Sleep(1e9) // one second
timeout <- true
}()
select {
case <-ch:
// a read from ch has occurred
case <-timeout:
// the read from ch has timed out
}
```
10如何使用输入通道和输出通道代替锁
```go
func Worker(in, out chan *Task) {
for {
t := <-in
process(t)
out <- t
}
}
```
11如何在同步调用运行时间过长时将之丢弃参考[章节14.5](14.5.md) 第二个变体
12如何在通道中使用计时器和定时器参考[章节14.5](14.5.md)
13典型的服务器后端模型参考[章节14.4](14.4.md)
## 链接
- [目录](directory.md)
- 上一章:[文件](18.7.md)
- 下一节:[网络和网页应用](18.9.md)