gpt4 book ai didi

concurrency - Go 超时 sleep 但不忙等待

转载 作者:IT王子 更新时间:2023-10-29 01:51:36 28 4
gpt4 key购买 nike

在 Go 中,我可以使用 time.After 使休眠函数超时,但我不能对忙等待(或工作)的函数执行相同的操作。以下代码在一秒后返回 timed out,然后挂起。

package main

import (
"fmt"
"time"
)

func main() {
sleepChan := make(chan int)
go sleep(sleepChan)
select {
case sleepResult := <-sleepChan:
fmt.Println(sleepResult)
case <-time.After(time.Second):
fmt.Println("timed out")
}

busyChan := make(chan int)
go busyWait(busyChan)
select {
case busyResult := <-busyChan:
fmt.Println(busyResult)
case <-time.After(time.Second):
fmt.Println("timed out")
}
}

func sleep(c chan<- int) {
time.Sleep(10 * time.Second)
c <- 0
}

func busyWait(c chan<- int) {
for {
}
c <- 0
}

为什么在第二种情况下不触发超时,我需要使用什么替代方法来中断工作中的 goroutines?

最佳答案

for {} 语句是一个独占单个处理器的无限循环。设置runtime.GOMAXPROCS到 2 或更多以允许计时器运行。

例如,

package main

import (
"fmt"
"runtime"
"time"
)

func main() {
fmt.Println(runtime.GOMAXPROCS(0))
runtime.GOMAXPROCS(runtime.NumCPU())
fmt.Println(runtime.GOMAXPROCS(0))
sleepChan := make(chan int)
go sleep(sleepChan)
select {
case sleepResult := <-sleepChan:
fmt.Println(sleepResult)
case <-time.After(time.Second):
fmt.Println("timed out")
}

busyChan := make(chan int)
go busyWait(busyChan)
select {
case busyResult := <-busyChan:
fmt.Println(busyResult)
case <-time.After(time.Second):
fmt.Println("timed out")
}
}

func sleep(c chan<- int) {
time.Sleep(10 * time.Second)
c <- 0
}

func busyWait(c chan<- int) {
for {
}
c <- 0
}

输出(4 CPU 处理器):

1
4
timed out
timed out

关于concurrency - Go 超时 sleep 但不忙等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265245/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com