неблокирующие каналы
This commit is contained in:
parent
80c656cd73
commit
3a7af42519
@ -26,7 +26,7 @@ Switch
|
|||||||
Направления канала (Channel Directions)
|
Направления канала (Channel Directions)
|
||||||
Select
|
Select
|
||||||
Тайм-ауты (Timeouts)
|
Тайм-ауты (Timeouts)
|
||||||
Non-Blocking Channel Operations
|
Неблокируемые операции в каналах (Non-Blocking Channel Operations)
|
||||||
Closing Channels
|
Closing Channels
|
||||||
Range over Channels
|
Range over Channels
|
||||||
Timers
|
Timers
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
// Basic sends and receives on channels are blocking.
|
// Стандартные отправители и получатели в каналах
|
||||||
// However, we can use `select` with a `default` clause to
|
// являются блокирующими.Тем не менее, мы можем
|
||||||
// implement _non-blocking_ sends, receives, and even
|
// использовать `select` с выбором `по умолчанию` для
|
||||||
// non-blocking multi-way `select`s.
|
// реализации _неблокирующих_ отправителей, получателей
|
||||||
|
// и даже неблокирующего множественного `select`'а.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -11,10 +12,10 @@ func main() {
|
|||||||
messages := make(chan string)
|
messages := make(chan string)
|
||||||
signals := make(chan bool)
|
signals := make(chan bool)
|
||||||
|
|
||||||
// Here's a non-blocking receive. If a value is
|
// Вот неблокирующее получение. Если значение
|
||||||
// available on `messages` then `select` will take
|
// доступно в `messages`, тогда `select` выберет
|
||||||
// the `<-messages` `case` with that value. If not
|
// `<-messages` с этим значением. Если нет, он
|
||||||
// it will immediately take the `default` case.
|
// сразу же примет случай `по-умолчанию`.
|
||||||
select {
|
select {
|
||||||
case msg := <-messages:
|
case msg := <-messages:
|
||||||
fmt.Println("received message", msg)
|
fmt.Println("received message", msg)
|
||||||
@ -22,10 +23,11 @@ func main() {
|
|||||||
fmt.Println("no message received")
|
fmt.Println("no message received")
|
||||||
}
|
}
|
||||||
|
|
||||||
// A non-blocking send works similarly. Here `msg`
|
// Неблокирующая отправка работает аналогично.
|
||||||
// cannot be sent to the `messages` channel, because
|
// Здесь `msg` не может быть отправлено в канал
|
||||||
// the channel has no buffer and there is no receiver.
|
// `messages`, потому что у канала нет буфера и
|
||||||
// Therefore the `default` case is selected.
|
// нет получателя. Поэтому выбирается действие
|
||||||
|
// `по-умолчанию`.
|
||||||
msg := "hi"
|
msg := "hi"
|
||||||
select {
|
select {
|
||||||
case messages <- msg:
|
case messages <- msg:
|
||||||
@ -34,10 +36,11 @@ func main() {
|
|||||||
fmt.Println("no message sent")
|
fmt.Println("no message sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can use multiple `case`s above the `default`
|
// Мы можем использовать несколько `case`'ов перед
|
||||||
// clause to implement a multi-way non-blocking
|
// `действием по-умолчанию` для реализации
|
||||||
// select. Here we attempt non-blocking receives
|
// многоцелевого неблокирующего выбора. Здесь мы
|
||||||
// on both `messages` and `signals`.
|
// пытаемся получить неблокирующее получение
|
||||||
|
// `messages` и `signals`.
|
||||||
select {
|
select {
|
||||||
case msg := <-messages:
|
case msg := <-messages:
|
||||||
fmt.Println("received message", msg)
|
fmt.Println("received message", msg)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user