Golang通道的无阻塞读写的方法示例
无论是无缓冲通道,还是有缓冲通道,都存在阻塞的情况,但其实有些情况,我们并不想读数据或者写数据阻塞在那里,有1个唯一的解决办法,那就是使用select结构。
这篇文章会介绍,哪些情况会存在阻塞,以及如何使用select解决阻塞。
阻塞场景
阻塞场景共4个,有缓存和无缓冲各2个。
无缓冲通道的特点是,发送的数据需要被读取后,发送才会完成,它阻塞场景:
通道中无数据,但执行读通道。
通道中无数据,向通道写数据,但无协程读取。
//场景1funcReadNoDataFromNoBufCh(){noBufCh:=make(chanint)<-noBufChfmt.Println("readfromnobufferchannelsuccess")//Output://fatalerror:allgoroutinesareasleep-deadlock!}//场景2funcWriteNoBufCh(){ch:=make(chanint)ch<-1fmt.Println("writesuccessnoblock")//Output://fatalerror:allgoroutinesareasleep-deadlock!}
注:示例代码中的Output注释代表函数的执行结果,每一个函数都由于阻塞在通道操作而无法继续向下执行,最后报了死锁错误。
有缓存通道的特点是,有缓存时可以向通道中写入数据后直接返回,缓存中有数据时可以从通道中读到数据直接返回,这时有缓存通道是不会阻塞的,它阻塞的场景是:
通道的缓存无数据,但执行读通道。
通道的缓存已经占满,向通道写数据,但无协程读。
//场景1funcReadNoDataFromBufCh(){bufCh:=make(chanint,1)<-bufChfmt.Println("readfromnobufferchannelsuccess")//Output://fatalerror:allgoroutinesareasleep-deadlock!}//场景2funcWriteBufChButFull(){ch:=make(chanint,1)//makechfullch<-100ch<-1fmt.Println("writesuccessnoblock")//Output://fatalerror:allgoroutinesareasleep-deadlock!}
使用Select实现无阻塞读写
select是执行选择操作的一个结构,它里面有一组case语句,它会执行其中无阻塞的那一个,如果都阻塞了,那就等待其中一个不阻塞,进而继续执行,它有一个default语句,该语句是永远不会阻塞的,我们可以借助它实现无阻塞的操作。
下面示例代码是使用select修改后的无缓冲通道和有缓冲通道的读写,以下函数可以直接通过main函数调用,其中的Ouput的注释是运行结果,从结果能看出,在通道不可读或者不可写的时候,不再阻塞等待,而是直接返回。
//无缓冲通道读funcReadNoDataFromNoBufChWithSelect(){bufCh:=make(chanint)ifv,err:=ReadWithSelect(bufCh);err!=nil{fmt.Println(err)}else{fmt.Printf("read:%d\n",v)}//Output://channelhasnodata}//有缓冲通道读funcReadNoDataFromBufChWithSelect(){bufCh:=make(chanint,1)ifv,err:=ReadWithSelect(bufCh);err!=nil{fmt.Println(err)}else{fmt.Printf("read:%d\n",v)}//Output://channelhasnodata}//select结构实现通道读funcReadWithSelect(chchanint)(xint,errerror){select{casex=<-ch:returnx,nildefault:return0,errors.New("channelhasnodata")}}//无缓冲通道写funcWriteNoBufChWithSelect(){ch:=make(chanint)iferr:=WriteChWithSelect(ch);err!=nil{fmt.Println(err)}else{fmt.Println("writesuccess")}//Output://channelblocked,cannotwrite}//有缓冲通道写funcWriteBufChButFullWithSelect(){ch:=make(chanint,1)//makechfullch<-100iferr:=WriteChWithSelect(ch);err!=nil{fmt.Println(err)}else{fmt.Println("writesuccess")}//Output://channelblocked,cannotwrite}//select结构实现通道写funcWriteChWithSelect(chchanint)error{select{casech<-1:returnnildefault:returnerrors.New("channelblocked,cannotwrite")}}
使用Select+超时改善无阻塞读写
使用default实现的无阻塞通道阻塞有一个缺陷:当通道不可读或写的时候,会即可返回。实际场景,更多的需求是,我们希望,尝试读一会数据,或者尝试写一会数据,如果实在没法读写,再返回,程序继续做其它的事情。
使用定时器替代default可以解决这个问题。比如,我给通道读写数据的容忍时间是500ms,如果依然无法读写,就即刻返回,修改一下会是这样:
funcReadWithSelect(chchanint)(xint,errerror){timeout:=time.NewTimer(time.Microsecond*500)select{casex=<-ch:returnx,nilcase<-timeout.C:return0,errors.New("readtimeout")}}funcWriteChWithSelect(chchanint)error{timeout:=time.NewTimer(time.Microsecond*500)select{casech<-1:returnnilcase<-timeout.C:returnerrors.New("writetimeout")}}
结果就会变成超时返回:
read time out
write time out
read time out
write time out
文章同步发布:https://www.geek-share.com/detail/2752320764.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。