英文:
Change a channel in select{case:channel}
问题
我使用一个 Ticker 来定期执行任务,但在更改它时遇到了一些问题。我会在接收到一些消息时将 Ticker 更改为新的,并更改时间间隔。以下是一个示例代码,可以重现这个问题:
package main
import (
"fmt"
"time"
)
type A struct {
ticker *time.Ticker
}
func (a *A) modify() {
a.ticker.Stop()
a.ticker = time.NewTicker(time.Second)
}
func main() {
a := new(A)
a.ticker = time.NewTicker(time.Second)
go func() {
for {
select {
case <-a.ticker.C:
fmt.Println("now")
go a.modify()
/*
default:
//fmt.Println("default")
time.Sleep(time.Millisecond * 100)
*/
}
}
}()
time.Sleep(time.Second * 60)
}
只会打印一次 "now"。但是如果我移除 "go",就会连续打印,像这样:
package main
import (
"fmt"
"time"
)
type A struct {
ticker *time.Ticker
}
func (a *A) modify() {
a.ticker.Stop()
a.ticker = time.NewTicker(time.Second)
}
func main() {
a := new(A)
a.ticker = time.NewTicker(time.Second)
go func() {
for {
select {
case <-a.ticker.C:
fmt.Println("now")
a.modify()
/*
default:
//fmt.Println("default")
time.Sleep(time.Millisecond * 100)
*/
}
}
}()
time.Sleep(time.Second * 60)
}
另外,如果我取消注释默认的分支,"now" 就会连续打印。有人能解释一下为什么会这样吗?
英文:
I use a Ticker to execute tasks periodically, but I've got some problems when changing it. I'll change the ticker to a new one on receiving some messages and change the interval. Here's the sample code which will repro this problem:
package main
import (
"fmt"
"time"
)
type A struct {
ticker *time.Ticker
}
func (a *A) modify() {
a.ticker.Stop()
a.ticker = time.NewTicker(time.Second)
}
func main() {
a := new(A)
a.ticker = time.NewTicker(time.Second)
go func() {
for {
select {
case <-a.ticker.C:
fmt.Println("now")
go a.modify()
/*
default:
//fmt.Println("default")
time.Sleep(time.Millisecond * 100)
*/
}
}
}()
time.Sleep(time.Second * 60)
}
"now" will be printed only once. But it will be printed continously if I remove the "go", like this:
package main
import (
"fmt"
"time"
)
type A struct {
ticker *time.Ticker
}
func (a *A) modify() {
a.ticker.Stop()
a.ticker = time.NewTicker(time.Second)
}
func main() {
a := new(A)
a.ticker = time.NewTicker(time.Second)
go func() {
for {
select {
case <-a.ticker.C:
fmt.Println("now")
a.modify()
/*
default:
//fmt.Println("default")
time.Sleep(time.Millisecond * 100)
*/
}
}
}()
time.Sleep(time.Second * 60)
}
Also, if I leave the default clause un-commented, "now" can be printed continously.
Can anyone explain how would this happen?
答案1
得分: 0
问题在于goroutine以异步方式运行。
当使用a.modify()
时,你的代码的行为如下:
- 从
a.ticker.C
获取tick。 - 停止旧的ticker,并创建新的
a.ticker.C
。 - 使用
select
等待a.ticker.C
。
在这种情况下,第2步中新创建的a.ticker.C
与第3步中等待的通道是相同的。
如果你在goroutine中执行第2步,可能会按照以下顺序执行:
- 从
a.ticker.C
获取tick。 - 使用
select
等待a.ticker.C
。 - 停止旧的ticker,并创建新的
a.ticker.C
。
在这种情况下,第2步中等待的通道与第3步中新创建的通道是不同的。
由于选择的通道是已停止的旧通道,它永远不会收到任何tick。
你可以通过插入一些fmt.Printf
并观察a.ticker.C
的地址来确认这种行为。
func (a *A) modify() {
a.ticker.Stop()
fmt.Printf("ticker stopped: %p\n", &a.ticker.C)
a.ticker = time.NewTicker(time.Second)
fmt.Printf("new ticker created: %p\n", &a.ticker.C)
}
func main() {
a := new(A)
a.ticker = time.NewTicker(time.Second)
go func() {
for {
fmt.Printf("waiting for ticker: %p\n", &a.ticker.C)
select {
....
使用a.modify()
的输出结果如下:
waiting for ticker: 0xc420010100
ticker stopped: 0xc420010100
new ticker created: 0xc420068000
waiting for ticker: 0xc420068000
ticker stopped: 0xc420068000
new ticker created: 0xc420068080
waiting for ticker: 0xc420068080
使用go a.modify()
的输出结果如下:
waiting for ticker: 0xc420010100
waiting for ticker: 0xc420010100
ticker stopped: 0xc420010100
new ticker created: 0xc420066040
你可以看到,使用go a.modify()
时,你不会等待新创建的通道。
关于默认行为的更新:
当在go a.modify()
中使用default:
时,它的行为如下:
- 等待
a.ticker.C
,收到tick,调用go a.modify()
执行第3步。 - 等待
a.ticker.C
,没有收到任何内容,所以回退到默认情况并休眠100毫秒。 - 停止旧的ticker,更新
a.ticker.C
。 - 等待
a.ticker.C
,没有收到任何内容,所以回退到默认情况并休眠100毫秒。 - 等待
a.ticker.C
,没有收到任何内容,所以回退到默认情况并休眠100毫秒。 - 等待
a.ticker.C
,没有收到任何内容,所以回退到默认情况并休眠100毫秒。
......
- 等待
a.ticker.C
,收到tick,调用go a.modify()
。
关键是for{}
循环可以继续进行,即使从a.ticker.C
中没有收到任何内容。
你可以使用相同的代码确认这种行为。
waiting ticker: 0xc420010100 <-- 1.
now <-- 1.
waiting ticker: 0xc420010100 <-- 2.
default <-- 2.
ticker stopped: 0xc420010100 <-- 3.
new ticker created: 0xc420066240 <-- 3.
waiting ticker: 0xc420066240 <-- 4.
default <-- 4.
英文:
The problem is that goroutine runs asynchronously.
your code behave like this when using a.modify()
:
- get tick from
a.ticker.C
- stop old ticker, and create new
a.ticker.C
- wait for
a.ticker.C
usingselect
In this case, newly created a.ticker.C
in 2. is identical to channel waiting in 3.
If you do 2. in goroutine. it may be done in following order
- get tick from
a.ticker.C
- wait for
a.ticker.C
usingselect
- stop old ticker, and create new
a.ticker.C
In this case channel waiting in 2. is different from newly created channel in 3. .
Since selecting channel is old one which is stopped, it never gets any tick.
You can confirm this behavior inserting some fmt.Printf
and watch for the address of a.ticker.C
.
func (a *A) modify() {
a.ticker.Stop()
fmt.Printf("ticker stopped: %p\n", &a.ticker.C)
a.ticker = time.NewTicker(time.Second)
fmt.Printf("new ticker created: %p\n", &a.ticker.C)
}
func main() {
a := new(A)
a.ticker = time.NewTicker(time.Second)
go func() {
for {
fmt.Printf("waiting for ticker: %p\n", &a.ticker.C)
select {
....
with a.modify()
:
waiting for ticker: 0xc420010100
ticker stopped: 0xc420010100
new ticker created: 0xc420068000
waiting for ticker: 0xc420068000
ticker stopped: 0xc420068000
new ticker created: 0xc420068080
waiting for ticker: 0xc420068080
with go a.modify()
:
waiting for ticker: 0xc420010100
waiting for ticker: 0xc420010100
ticker stopped: 0xc420010100
new ticker created: 0xc420066040
you can see that with go a.modify()
you are not waiting for a newly created channel.
UPDATE for behavior of default:
When using default:
with go a.modify()
, it will behave like this.
- wait for
a.ticker.C
, got tick, callgo a.modify()
which does 3. - wait for
a.ticker.C
, got nothing, so fallback to default and sleep 100ms. - stop old ticker, update
a.ticker.C
- wait for
a.ticker.C
, got nothing, so fallback to default and sleep 100ms. - wait for
a.ticker.C
, got nothing, so fallback to default and sleep 100ms. - wait for
a.ticker.C
, got nothing, so fallback to default and sleep 100ms.
.....
- wait for
a.ticker.C
, got tick, callgo a.modify()
The point is that for{}
loop can keep on going even when you got nothing from a.ticker.C
.
You can confirm the behavior with same code.
waiting ticker: 0xc420010100 <-- 1.
now <-- 1.
waiting ticker: 0xc420010100 <-- 2.
default <-- 2.
ticker stopped: 0xc420010100 <-- 3.
new ticker created: 0xc420066240 <-- 3.
waiting ticker: 0xc420066240 <-- 4.
default <-- 4.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论