Channel

1. 概述

“网络,并发”是Go语言的两大feature。Go语言号称“互联网的C语言”,与使用传统的C语言相比,写一个Server所使用的代码更少,也更简单。写一个Server除了网络,另外就是并发,相对python等其它语言,Go对并发支持使得它有更好的性能。

Goroutine和channel是Go在“并发”方面两个核心feature。

Channel是goroutine之间进行通信的一种方式,它与Unix中的管道类似。

Channel声明:

ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType .

例如:

var ch chan int

var ch1 chan<- int  //ch1只能写

var ch2 <-chan int  //ch2只能读

channel是类型相关的,也就是一个channel只能传递一种类型。例如,上面的ch只能传递int。

在go语言中,有4种引用类型:slice,map,channel,interface。

Slice,map,channel一般都通过make进行初始化:

ci := make(chan int)            // unbuffered channel of integers

cj := make(chan int, 0)         // unbuffered channel of integers

cs := make(chan *os.File, 100)  // buffered channel of pointers to Files

创建channel时可以提供一个可选的整型参数,用于设置该channel的缓冲区大小。该值缺省为0,用来构建默认的“无缓冲channel”,也称为“同步channel”。

Channel作为goroutine间的一种通信机制,与操作系统的其它通信机制类似,一般有两个目的:同步,或者传递消息。

2. 同步

c := make(chan int)  // Allocate a channel.

// Start the sort in a goroutine; when it completes, signal on the channel.

go func() {

list.Sort()

c <- 1  // Send a signal; value does not matter.

}()

doSomethingForAWhile()

<-c   // Wait for sort to finish; discard sent value.

上面的示例中,在子goroutine中进行排序操作,主goroutine可以做一些别的事情,然后等待子goroutine完成排序。

接收方会一直阻塞直到有数据到来。如果channel是无缓冲的,发送方会一直阻塞直到接收方将数据取出。如果channel带有缓冲区,发送方会一直阻塞直到数据被拷贝到缓冲区;如果缓冲区已满,则发送方只能在接收方取走数据后才能从阻塞状态恢复。

3. 消息传递

我们来模拟一下经典的生产者-消费者模型。

func Producer (queue chan<- int){

for i:= 0; i < 10; i++ {

queue <- i

}

}

func Consumer( queue <-chan int){

for i :=0; i < 10; i++{

v := <- queue

fmt.Println("receive:", v)

}

}

func main(){

queue := make(chan int, 1)

go Producer(queue)

go Consumer(queue)

time.Sleep(1e9) //让Producer与Consumer完成

}

上面的示例在Producer中生成数据,在Consumer中处理数据。

4. Server编程模型

在server编程,一种常用的模型:主线程接收请求,然后将请求分发给工作线程,工作线程完成请求处理。用go来实现,如下:

func handle(r *Request) {

process(r)  // May take a long time.

}

func Serve(queue chan *Request) {

for {

req := <-queue

go handle(req)  // Don't wait for handle to finish.

}

}

一般来说,server的处理能力不是无限的,所以,有必要限制线程(或者goroutine)的数量。在C/C++编程中,我们一般通过信号量来实现,在go中,我们可以通过channel达到同样的效果:

var sem = make(chan int, MaxOutstanding)

func handle(r *Request) {

sem <- 1    // Wait for active queue to drain.

process(r)  // May take a long time.

<-sem       // Done; enable next request to run.

}

func Serve(queue chan *Request) {

for {

req := <-queue

go handle(req)  // Don't wait for handle to finish.

}

}

我们通过引入sem channel,限制了同时最多只有MaxOutstanding个goroutine运行。但是,上面的做法,只是限制了运行的goroutine的数量,并没有限制goroutine的生成数量。如果请求到来的速度过快,会导致产生大量的goroutine,这会导致系统资源消耗完全。

为此,我们有必要限制goroutine的创建数量:

func Serve(queue chan *Request) {

for req := range queue {

sem <- 1

go func() {

process(req) // Buggy; see explanation below.

<-sem

}()

}

}

上面的代码看似简单清晰,但在go中,却有一个问题。Go语言中的循环变量每次迭代中是重用的,更直接的说就是req在所有的子goroutine中是共享的,从变量的作用域角度来说,变量req对于所有的goroutine,是全局的。

这个问题属于语言实现的范畴,在C语言中,你不应该将一个局部变量传递给另外一个线程去处理。有很多解决方法,这里有一个讨论。从个人角度来说,我更倾向下面这种方式:

func Serve(queue chan *Request) {

for req := range queue {

sem <- 1

go func(r *Request) {

process(r)

<-sem

}(req)

}

}

至少,这样的代码不会让一个go的初学者不会迷糊,另外,从变量的作用域角度,也更符合常理一些。

在实际的C/C++编程中,我们倾向于工作线程在一开始就创建好,而且线程的数量也是固定的。在go中,我们也可以这样做:

func handle(queue chan *Request) {

for r := range queue {

process(r)

}

}

func Serve(clientRequests chan *Request, quit chan bool) {

// Start handlers

for i := 0; i < MaxOutstanding; i++ {

go handle(clientRequests)

}

<-quit  // Wait to be told to exit.

}

开始就启动固定数量的handle goroutine,每个goroutine都直接从channel中读取请求。这种写法比较简单,但是不知道有没有“惊群”问题?有待后续分析goroutine的实现。

5. 传递channel的channel

channel作为go语言的一种原生类型,自然可以通过channel进行传递。通过channel传递channel,可以非常简单优美的解决一些实际中的问题。

在上一节中,我们主goroutine通过channel将请求传递给工作goroutine。同样,我们也可以通过channel将处理结果返回给主goroutine。

主goroutine:

type Request struct {

args        []int

resultChan  chan int

}

request := &Request{[]int{3, 4, 5}, make(chan int)}

// Send request

clientRequests <- request

// Wait for response.

fmt.Printf("answer: %d\n", <-request.resultChan)

主goroutine将请求发给request channel,然后等待result channel。子goroutine完成处理后,将结果写到result channel。

func handle(queue chan *Request) {

for req := range queue {

result := do_something()

req.resultChan <- result

}

}

6. 多个channel

在实际编程中,经常会遇到在一个goroutine中处理多个channel的情况。我们不可能阻塞在两个channel,这时就该select场了。与C语言中的select可以监控多个fd一样,go语言中select可以等待多个channel。

c1 := make(chan string)

c2 := make(chan string)

go func() {

time.Sleep(time.Second * 1)

c1 <- "one"

}()

go func() {

time.Sleep(time.Second * 2)

c2 <- "two"

}()

for i := 0; i < 2; i++ {

select {

case msg1 := <-c1:

fmt.Println("received", msg1)

case msg2 := <-c2:

fmt.Println("received", msg2)

}

}

在C中,我们一般都会传一个超时时间给select函数,go语言中的select没有该参数,相当于超时时间为0。

主要参考

https://golang.org/doc/effective_go.html

作者:YY哥 
出处:http://www.cnblogs.com/hustcat/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

深入学习golang(2)—channel的更多相关文章

  1. golang的Channel

    golang的Channel Channel 是 golang 一个非常重要的概念,如果你是刚开始使用 golang 的开发者,你可能还没有真正接触这一概念,本篇我们将分析 golang 的Chann ...

  2. c#实现golang 的channel

    使用.NET的 BlockingCollection<T>来包装一个ConcurrentQueue<T>来实现golang的channel. 代码如下: public clas ...

  3. golang的channel实现

    golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.s ...

  4. 学习Golang语言(6):类型--切片

    学习Golang语言(1): Hello World 学习Golang语言(2): 变量 学习Golang语言(3):类型--布尔型和数值类型 学习Golang语言(4):类型--字符串 学习Gola ...

  5. 前端程序员学习 Golang gin 框架实战笔记之一开始玩 gin

    原文链接 我是一名五六年经验的前端程序员,现在准备学习一下 Golang 的后端框架 gin. 以下是我的学习实战经验,记录下来,供大家参考. https://github.com/gin-gonic ...

  6. Golang学习笔记:channel

    channel channel是goroutine之间的通信机制,它可以让一个goroutine通过它给另一个goroutine发送数据,每个channel在创建的时候必须指定一个类型,指定的类型是任 ...

  7. 【GoLang】golang context channel 详解

    代码示例: package main import ( "fmt" "time" "golang.org/x/net/context" ) ...

  8. golang中channel的超时处理

    并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...

  9. 如何优雅的关闭golang的channel

    How to Gracefully Close Channels,这篇博客讲了如何优雅的关闭channel的技巧,好好研读,收获良多. 众所周知,在golang中,关闭或者向已关闭的channel发送 ...

随机推荐

  1. Awesome Machine Learning

    https://github.com/josephmisiti/awesome-machine-learning 包含了很多的machine-learning开源的资源,包括各种语言的machin l ...

  2. javascript第四节其它引用对象

    单体对象 Global对象(全局)这个对象不存在,无形的对象 其内部定义了一些方法和属性:encodeURL.encodeURIComponent.decodeURI.decodeURICompone ...

  3. ubuntu下完全安装mcrypt

    源文章: ubuntu下安装mcrypt 1.首先要下载三个软件 0libmcrypt-2.5.8.tar.gz 下载地址:http://sourceforge.net/project/showfil ...

  4. perl 对源文件内容修改 方法整理

    1, 利用Tie::File模块来直接对文件内容进行修改. #!/usr/bin/perl -w my $std="F160"; my $fast="FAST" ...

  5. Action类为何要继承ActionSupport

    Action类为何要继承ActionSupport   理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承com.open ...

  6. 在centos上编译安装mariadb数据库

    一.安装前提(准备数据文件.安装其他依赖的软件) 1.准备数据存放的目录 [root@localhost ~]# fdisk /dev/sdb  (fdisk /dev/sdb 创建一个逻辑分区/de ...

  7. Masonry 轻量级布局框架的使用

    iOS 提供了自动布局的方法,但是原生的方法使用太过麻烦 ,Masonry 框架提供了类似的方法,同样可以实现自动布局 ,代码更加直观,而且容易理解. Masonry 是一个轻量级的布局框架.拥有自己 ...

  8. github 仓库管理

    一.远程仓库有master和dev分支1. 克隆代码 git clone https://github.com/master-dev.git # 这个git路径是无效的,示例而已 2. 查看所有分支 ...

  9. 安装完win8后,显卡一些其他的驱动没有被正常安装,此处出现问题的机器是索尼vpccw18fc

    问题描述:安装完win8后机器无法正常开启,通过索尼欢迎页面按f10,修复进入,进入后一切正常,分辨率怪异,通过鲁大师检测显卡驱动以及一些细微的驱动没安装,但是鲁大师提示不支持这款显卡驱动,官网也没找 ...

  10. 直接请求URL调用 axis webservices

    假设 http://127.0.0.1/services/Services?wsdl 有名称为 login 方法,且参数为 name , pwd 则,URL请求如下 http://127.0.0.1/ ...