Files
the-way-to-go_ZH_CN/eBook/18.8.md
Haigang Zhou fa1cfcc67f 第十七十八章 (#833)
Co-authored-by: Joe Chen <jc@unknwon.io>
2022-05-19 19:57:23 +08:00

119 lines
3.2 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)
出于性能考虑的建议:
实践经验表明,为了使并行运算获得高于串行运算的效率,在协程内部完成的工作量,必须远远高于协程的创建和相互来回通信的开销。
- 出于性能考虑建议使用带缓存的通道:
使用带缓存的通道可以很轻易成倍提高它的吞吐量,某些场景其性能可以提高至 10 倍甚至更多。通过调整通道的容量,甚至可以尝试着更进一步的优化其性能。
- 限制一个通道的数据数量并将它们封装成一个数组:
如果使用通道传递大量单独的数据,那么通道将变成性能瓶颈。然而,将数据块打包封装成数组,在接收端解压数据时,性能可以提高至 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通道迭代器模板这里原书没有写东西但是应该是参考[章节 14.2.10](14.2.md)
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)