处理前景中的信号
我想在背景中听一个OS信号,但在前景中处理它。
这是示例代码。 Mainloop每一秒无限期地打印“ Boop”。 但是,当收到中断信号时,处理程序会打印“缓慢终止……”,然后等待五秒钟,然后终止程序。
package main
import (
"fmt"
"os"
"os/signal"
"time"
)
func main() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("Terminating slowly...")
time.Sleep(time.Duration(5)*time.Second)
os.Exit(0)
}()
for {
fmt.Println("boop")
time.Sleep(time.Duration(1)*time.Second)
}
}
当信号处理时,我希望其他所有内容都可以阻止。 但是目前,在缓慢终止的五秒钟内,它一直在打印“ boop”。
我明白了:
boop
boop
^CTerminating slowly...
boop
boop
boop
boop
boop
我想要这样:
boop
boop
^CTerminating slowly...
真正的程序是一个基于堆栈的语言解释器,用户可以在终止时运行某些东西,但是当前Mainloop可以同时更改堆栈。
I would like to listen for a OS signal in the background but handle it in the foreground.
Here's example code.
The mainloop prints "boop" every second indefinitely.
But when an interrupt signal is received, the handler prints "Terminating slowly..." and waits five seconds before terminating the program.
package main
import (
"fmt"
"os"
"os/signal"
"time"
)
func main() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("Terminating slowly...")
time.Sleep(time.Duration(5)*time.Second)
os.Exit(0)
}()
for {
fmt.Println("boop")
time.Sleep(time.Duration(1)*time.Second)
}
}
When the signal is being handled, I want everything else to block.
But currently during the five seconds of slow termination, it keeps printing "boop".
I get this:
boop
boop
^CTerminating slowly...
boop
boop
boop
boop
boop
I'd like this:
boop
boop
^CTerminating slowly...
The real program is a stack-based-language interpreter where the user can have something run on termination, but currently the mainloop can change the stack at the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管
选择
答案绝对是最好的,但我只想补充说,如果您会翻转逻辑,则结果可能还可以:While the
select
answer is definitely the best, I just want to add that if you would have flipped the logic, the result would have been probably OK:AS mh-cbon oper >选择语句是您需要的。一旦将循环的主体包裹在A
选择
的默认情况下,就不再需要启动另一个goroutine;使用选择
案例来完成此工作。另外,您正在使用一个未封闭的通道,但是根据
notify
notify ' s文档,该功能期望适当缓冲通道:一旦信号发送到频道
c
,非默认情况就会变为非阻滞,并获取选择
ed,函数结束。这是程序的一个可能输出(在该特定执行期间,我在大约4秒后点击
^c
):As mh-cbon points out, a
select
statement is what you need, here. Once you wrap the body of your loop inside the default case of aselect
, you no longer need to start another goroutine; use aselect
case to do that work.Also, you're using an unbuffered channel, but according to
Notify
's documentation, that function expects an appropriately buffered channel:As soon as a signal is sent to channel
c
, the non-default case becomes non-blocking and getsselect
ed, and the function ends.Here is one possible output of the program (during that specific execution, I hit
^C
after about 4 seconds):