// select.go
package main
import (
"fmt"
"time"
//"time"
)
func main() {
//声明一个channel
ch := make(chan int)
//声明一个匿名函数,传入一个参数整型channel类型ch
go func(ch chan int) {
ch <- 1
//往channel写入一个数据,此时阻塞
}(ch)
//由于goroutine执行太快,先让它sleep 1秒
time.Sleep(time.Second)
select {
//读取ch,解除阻塞
case <-ch:
fmt.Print("come to read ch!")
default:
fmt.Print("come to default!")
}
}
// select.go
//整型channel类型ch一直处于读取状态,所以处于阻塞,使用select实现超时控制
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
//buffer channel,1个元素前非阻塞
timeout := make(chan int, 1)
go func() {
time.Sleep(time.Second)
//写channel
timeout <- 1
}()
select {
//读channel
case <-ch:
fmt.Print("come to read ch!")
//没有读到channel,实现超时控制
case <-timeout:
fmt.Print("come to timeout!")
}
fmt.Print("end of code!")
}
// select.go
//使用time.After实现超时控制
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
select {
case <-ch:
fmt.Print("come to read ch!")
case <-time.After(time.Second):
fmt.Print("come to timeout!")
}
fmt.Print("end of code!")
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python神经网络编程
[英]塔里克·拉希德(Tariq Rashid) / 林赐 / 人民邮电出版社 / 2018-4 / 69.00元
神经网络是一种模拟人脑的神经网络,以期能够实现类人工智能的机器学习 技术。 本书揭示神经网络背后的概念,并介绍如何通过Python实现神经网络。全书 分为3章和两个附录。第1章介绍了神经网络中所用到的数学思想。第2章介绍使 用Python实现神经网络,识别手写数字,并测试神经网络的性能。第3章带领读 者进一步了解简单的神经网络,观察已受训练的神经网络内部,尝试进一步改......一起来看看 《Python神经网络编程》 这本书的介绍吧!