go - 信号 goroutine 在通道关闭时停止
问题描述
我有select
来自两个通道的多个 goroutine:一个通道提供数据,一个通道用于信号(一种完成/退出通道)。
我使用信号通道来捕获信号(杀死)并优雅地关闭 goroutine。
我从 运行 'worker' goroutines package a
,而捕获信号的 goroutine 函数从 运行package b
。
我使用来自https://gist.github.com/reiki4040/be3705f307d3cd136e85的信号包。
package a
import "sync"
WorkChan := make(chan int)
QuitChan := make(chan struct{})
func Stop() {
fmt.Println("Stop called, closing channel")
close(QuitChan)
}
func Work(wg *sync.WaitGroup) {
var item int
for {
select {
case item = <- WorkChan:
... processing
case <- QuitChan:
wg.Done()
return
}
}
}
捕获信号并调用的 goroutinea.Stop()
package b
import (
"os/signal"
"os"
"syscal"
"a"
)
func Signal() {
sChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-sChan
switch s {
case os.Interrupt, syscall.SIGTERM:
a.Stop()
}
}
}
这是我的主要功能
package main
import (
"a"
"b"
"sync"
)
func main() {
var wg sync.WaitGroup
go b.Signal()
wg.Add(1) // for simplicity; actual code start multiple goroutines of Work
go a.Work(&wg)
// wait until work is done
wg.Wait()
fmt.Println("Done.")
}
当我终止正在运行的进程时,我会看到来自Quit
. 我预计一旦通道关闭,goroutines 会select
在QuitChan
某个时候返回。
但他们继续奔跑;他们继续处理来自WorkChan
. 似乎它被忽略了。我在这里想念什么?通道不会关闭吗?怎么还开着?
解决方案
首先我认为你应该做一个简单的测试,然后通过它。让其他人了解您的问题将更有帮助。
我改变了你的代码,让它读起来像一个 go 代码,而不是其他语言。现在它起作用了。
在您的代码中,有一些错误,我将其标记为 ERROR 注释。有些是语法错误,例如创建WorkChan
. 有些是类型错误。
导入设计你应该知道的一件事,当你想在执行后退出时Stop()
,你应该关闭WorkChan
你发送数据的地方WorkChan
,取而代之的是在你收到日期的地方返回。
前
package a import ( "fmt" "sync" ) // ERROR: can not do make in global var WorkChan chan int var QuitChan chan struct{} // Create chan when init func init() { fmt.Println("Init a") WorkChan = make(chan int) QuitChan = make(chan struct{}) } func Stop() { fmt.Println("Stop called, closing quit channel") close(QuitChan) } // Close the work channel where you send date func Start(wg *sync.WaitGroup) { i := 0 for { select { case <-QuitChan: fmt.Println("Closing work chan") close(WorkChan) wg.Done() return default: WorkChan <- i i++ } } } // Work will exit when workchan closed func Work(wg *sync.WaitGroup) { for item := range WorkChan { fmt.Printf("Receive %d\n", item) } wg.Done() fmt.Println("Work exit") }
b.go
package b import ( "github.com/shitaibin/awesome/a" "os" "os/signal" "syscall" ) func Signal() { sChan := make(chan os.Signal, 1) signal.Notify(sChan, syscall.SIGTERM, syscall.SIGINT) // ERROR for { s := <-sChan switch s { case os.Interrupt, syscall.SIGTERM: a.Stop() return // should return free resource } } }
main.go
package main import ( "fmt" "github.com/shitaibin/awesome/a" "github.com/shitaibin/awesome/b" "sync" ) func main() { var wg sync.WaitGroup go b.Signal() wg.Add(1) // for simplicity; actual code start multiple goroutines of Work go a.Work(&wg) // ERROR: pointer wg.Add(1) go a.Start(&wg) // Send data and close channel when stop // wait until work is done wg.Wait() fmt.Println("Done.") }
结果
// omit Receive 87028 Receive 87029 Receive 87030 Receive 87031 Receive 87032 Receiv^C101 <---- send signal here Receive 87102 Receive 87103 Receive 87104 Receive 87105 Receive 87106 Receive 87107 Receive 87108 Receive 87109 Receive 87110 Stop called, closing quit channel Receive 87111 Receive 87112 Closing work chan Work exit Done.
推荐阅读
- javafx - 变色单选按钮
- c - 如何用C实时读取键盘输入?
- javascript - 为什么这个函数不删除元音?
- java - 从不一致的 Android 本机文件选取返回的 URI 中获取 URI 文件路径
- android - 连接/断开 USB BroadcastReceiver 时隐藏/取消隐藏应用程序
- javascript - 仅构建 scss 文件到 css 防止编译/构建 JS 或仅构建 js,同时防止使用 webpack 4 编译 scss
- excel - VBA 集合检索数据但不显示值
- python - 有没有更好的方法来重新采样存储在 Python 中的二进制文件中的时间序列?
- angular - Angular Http拦截器无法使用localStorage获取用户令牌以添加到标头
- python - 在 macOS Sierra 上使用 git-review 的 RequestsDependencyWarning