是否可以从外部函数中停止 ticker/goroutine?
Is it possible to stop a ticker/goroutine from an outside function?
所以,我有这个(只是一个例子):
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ticker.C:
fmt.Println("hello")
}
}
}
这是一个无限循环,我希望这样。在实际代码中,它每 1 小时循环一次。
但是,如果我想调用一个函数让它停止循环怎么办?这可能吗?
类似于:
func stop() {
//this will stop the main function from looping
}
我知道我可以做类似的事情:
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
fmt.Println("done")
ticker.Stop()
return
case <-ticker.C:
fmt.Println("hello")
}
}
}()
time.Sleep(10 * time.Second)
done <- true
}
但这会在预定义的时间范围内停止该功能(在本例中为 10 秒),这不是我想要的,而且这都在同一功能内,我需要从外部进行调用主要功能。
这有可能吗?
这里:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func stop(ch chan<- struct{}) {
select {
// Triggers after 7 seconds
case <-time.After(7 * time.Second):
ch <- struct{}{}
}
}
func signalStop(ch chan<- struct{}) {
// Use a buffered channel (size = 1)
sig := make(chan os.Signal, 1)
// Use SIGINT signal i.e., <kill -SIGINT XXXX> or <Control+C>
signal.Notify(sig, syscall.SIGINT)
select {
// Capture the SIGINT signal
case <-sig:
// Close the channel
close(ch)
}
}
func main() {
ticker := time.NewTicker(1 * time.Second)
done := make(chan struct{})
// Spawn a goroutine with the done channel
go signalStop(done)
for {
select {
case <-ticker.C:
fmt.Println("Hello!")
case <-done:
// When stop signals, stop the ticker and return
ticker.Stop()
fmt.Println("Bye, bye!")
return
}
}
}
我已经对代码的相关部分进行了注释,以便您了解我正在尝试做什么。
所以,我有这个(只是一个例子):
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ticker.C:
fmt.Println("hello")
}
}
}
这是一个无限循环,我希望这样。在实际代码中,它每 1 小时循环一次。 但是,如果我想调用一个函数让它停止循环怎么办?这可能吗? 类似于:
func stop() {
//this will stop the main function from looping
}
我知道我可以做类似的事情:
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
fmt.Println("done")
ticker.Stop()
return
case <-ticker.C:
fmt.Println("hello")
}
}
}()
time.Sleep(10 * time.Second)
done <- true
}
但这会在预定义的时间范围内停止该功能(在本例中为 10 秒),这不是我想要的,而且这都在同一功能内,我需要从外部进行调用主要功能。
这有可能吗?
这里:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func stop(ch chan<- struct{}) {
select {
// Triggers after 7 seconds
case <-time.After(7 * time.Second):
ch <- struct{}{}
}
}
func signalStop(ch chan<- struct{}) {
// Use a buffered channel (size = 1)
sig := make(chan os.Signal, 1)
// Use SIGINT signal i.e., <kill -SIGINT XXXX> or <Control+C>
signal.Notify(sig, syscall.SIGINT)
select {
// Capture the SIGINT signal
case <-sig:
// Close the channel
close(ch)
}
}
func main() {
ticker := time.NewTicker(1 * time.Second)
done := make(chan struct{})
// Spawn a goroutine with the done channel
go signalStop(done)
for {
select {
case <-ticker.C:
fmt.Println("Hello!")
case <-done:
// When stop signals, stop the ticker and return
ticker.Stop()
fmt.Println("Bye, bye!")
return
}
}
}
我已经对代码的相关部分进行了注释,以便您了解我正在尝试做什么。