Golang并发:一招掌握无阻塞通道读写

栏目: Go · 发布时间: 5年前

内容简介:介绍Golang并发的模型写了几篇了,但一直没有以无论是有缓存通道、无缓冲通道都存在阻塞的情况。阻塞场景共4个,有缓存和无缓冲各2个。无缓冲通道的特点是,发送的数据需要被读取后,发送才会完成,它

介绍Golang并发的模型写了几篇了,但一直没有以 channel 为主题进行介绍,今天就给大家聊一聊channel,channel的基本使用非常简单,想必大家都已了解,所以直接来个进阶点的: 介绍channel的阻塞情况,以及给你一个必杀技,立马解决阻塞问题,实用性高

阻塞场景

无论是有缓存通道、无缓冲通道都存在阻塞的情况。阻塞场景共4个,有缓存和无缓冲各2个。

无缓冲通道的特点是,发送的数据需要被读取后,发送才会完成,它 阻塞场景

  1. 通道中无数据,但执行读通道。
  2. 通道中无数据,向通道写数据,但无协程读取。
// 场景1
func ReadNoDataFromNoBufCh() {
    noBufCh := make(chan int)

    <-noBufCh
    fmt.Println("read from no buffer channel success")

    // Output:
    // fatal error: all goroutines are asleep - deadlock!
}

// 场景2
func WriteNoBufCh() {
    ch := make(chan int)

    ch <- 1
    fmt.Println("write success no block")
    
    // Output:
    // fatal error: all goroutines are asleep - deadlock!
}

注:示例代码中的Output注释代表函数的执行结果,每一个函数都由于阻塞在通道操作而无法继续向下执行,最后报了死锁错误。

有缓存通道的特点是,有缓存时可以向通道中写入数据后直接返回,缓存中有数据时可以从通道中读到数据直接返回,这时有缓存通道是不会阻塞的,它 阻塞场景是

  1. 通道的缓存无数据,但执行读通道。
  2. 通道的缓存已经占满,向通道写数据,但无协程读。
// 场景1
func ReadNoDataFromBufCh() {
    bufCh := make(chan int, 1)

    <-bufCh
    fmt.Println("read from no buffer channel success")

    // Output:
    // fatal error: all goroutines are asleep - deadlock!
}

// 场景2
func WriteBufChButFull() {
    ch := make(chan int, 1)
    // make ch full
    ch <- 100

    ch <- 1
    fmt.Println("write success no block")
    
    // Output:
    // fatal error: all goroutines are asleep - deadlock!
}

使用Select实现无阻塞读写

select是执行选择操作的一个结构,它里面有一组case语句,它会执行其中无阻塞的那一个,如果都阻塞了,那就等待其中一个不阻塞,进而继续执行, 它有一个default语句,该语句是永远不会阻塞的,我们可以借助它实现无阻塞的操作 。如果不了解,不想多了解一下select可以先看下这2篇文章:

下面 示例代码是使用select修改后的无缓冲通道和有缓冲通道的读写 ,以下函数可以直接通过main函数调用,其中的Ouput的注释是运行结果,从结果能看出,在通道不可读或者不可写的时候,不再阻塞等待,而是直接返回。

// 无缓冲通道读
func ReadNoDataFromNoBufChWithSelect() {
    bufCh := make(chan int)

    if v, err := ReadWithSelect(bufCh); err != nil {
        fmt.Println(err)
    } else {
        fmt.Printf("read: %d\n", v)
    }

    // Output:
    // channel has no data
}

// 有缓冲通道读
func ReadNoDataFromBufChWithSelect() {
    bufCh := make(chan int, 1)

    if v, err := ReadWithSelect(bufCh); err != nil {
        fmt.Println(err)
    } else {
        fmt.Printf("read: %d\n", v)
    }

    // Output:
    // channel has no data
}

// select结构实现通道读
func ReadWithSelect(ch chan int) (x int, err error) {
    select {
    case x = <-ch:
        return x, nil
    default:
        return 0, errors.New("channel has no data")
    }
}

// 无缓冲通道写
func WriteNoBufChWithSelect() {
    ch := make(chan int)
    if err := WriteChWithSelect(ch); err != nil {
        fmt.Println(err)
    } else {
        fmt.Println("write success")
    }

    // Output:
    // channel blocked, can not write
}

// 有缓冲通道写
func WriteBufChButFullWithSelect() {
    ch := make(chan int, 1)
    // make ch full
    ch <- 100
    if err := WriteChWithSelect(ch); err != nil {
        fmt.Println(err)
    } else {
        fmt.Println("write success")
    }

    // Output:
    // channel blocked, can not write
}

// select结构实现通道写
func WriteChWithSelect(ch chan int) error {
    select {
    case ch <- 1:
        return nil
    default:
        return errors.New("channel blocked, can not write")
    }
}

使用Select+超时改善无阻塞读写

使用default实现的无阻塞通道阻塞有一个缺陷:当通道不可读或写的时候,会即可返回。实际场景,更多的需求是,我们希望尝试读一会数据,或者尝试写一会数据,如果实在没法读写再返回,程序继续做其它的事情。

使用定时器替代default可以解决这个问题, 给通道增加读写数据的容忍时间 ,如果500ms内无法读写,就即刻返回。示例代码修改一下会是这样:

func ReadWithSelect(ch chan int) (x int, err error) {
    timeout := time.NewTimer(time.Microsecond * 500)

    select {
    case x = <-ch:
        return x, nil
    case <-timeout.C:
        return 0, errors.New("read time out")
    }
}

func WriteChWithSelect(ch chan int) error {
    timeout := time.NewTimer(time.Microsecond * 500)

    select {
    case ch <- 1:
        return nil
    case <-timeout.C:
        return errors.New("write time out")
    }
}

结果就会变成超时返回:

read time out
write time out
read time out
write time out

示例源码

本文所有示例源码,及历史文章、代码都存储在Github: https://github.com/Shitaibin/...

这篇文章了channel的阻塞情况,以及解决阻塞的2种办法:

  1. 使用select的default语句,在channel不可读写时,即可返回
  2. 使用select+定时器,在超时时间内,channel不可读写,则返回

希望这篇文章对你的channel读写有所启发。

  1. 如果这篇文章对你有帮助,请点个赞/喜欢,感谢
  2. 本文作者: 大彬
  3. 如果喜欢本文,随意转载,但请保留此原文链接: http://lessisbetter.site/2018/11/03/Golang-channel-read-and-write-without-blocking/

Golang并发:一招掌握无阻塞通道读写


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ASP.NET揭秘

ASP.NET揭秘

Stephen Walther、汤涛 / 汤涛 / 中国电力出版社 / 2004-8-1 / 95.00元

本书是美国亚马逊网站同类书长期销售冠军,并受到微软ASP.NET小组项目经理Rob Howard的大力推荐,中文版由中科院专家汤涛老师翻译,经典、权威是本书最好的诠释。 本书共分10部分,31章,囊括了在.NET框架下架建ASP.NET应用程序的各个层面。每一章也都不是泛泛而谈理论,而是围绕实际样例代码来组织,让读者马上可以上手,并且加深理解。书中还包含了两个完整的、立即就可以用得......一起来看看 《ASP.NET揭秘》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具