处理前景中的信号

发布于 2025-01-25 09:45:33 字数 833 浏览 3 评论 0原文

我想在背景中听一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2025-02-01 09:45:34

尽管选择答案绝对是最好的,但我只想补充说,如果您会翻转逻辑,则结果可能还可以:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {
    c := make(chan os.Signal)
    signal.Notify(c, os.Interrupt)
    go func() {
        for {
            fmt.Println("boop")
            time.Sleep(time.Duration(1) * time.Second)
        }
    }()

    <-c
    fmt.Println("Terminating slowly...")
    time.Sleep(time.Duration(5) * time.Second)
    os.Exit(0)
}

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:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {
    c := make(chan os.Signal)
    signal.Notify(c, os.Interrupt)
    go func() {
        for {
            fmt.Println("boop")
            time.Sleep(time.Duration(1) * time.Second)
        }
    }()

    <-c
    fmt.Println("Terminating slowly...")
    time.Sleep(time.Duration(5) * time.Second)
    os.Exit(0)
}
疯了 2025-02-01 09:45:33

AS mh-cbon oper >选择语句是您需要的。一旦将循环的主体包裹在A 选择的默认情况下,就不再需要启动另一个goroutine;使用选择案例来完成此工作。

另外,您正在使用一个未封闭的通道,但是根据 notify notify ' s文档,该功能期望适当缓冲通道:

软件包信号不会阻止发送到C:呼叫者必须确保C具有足够的缓冲空间以跟上预期信号速率。对于仅通知一个信号值的通道,大小1的缓冲区就足够了。

package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {
    c := make(chan os.Signal, 1) // buffered
    signal.Notify(c, os.Interrupt)
    for {
        select {
        case <-c:
            fmt.Println("Terminating slowly...")
            time.Sleep(time.Duration(5) * time.Second)
            return
        default:
            fmt.Println("boop")
            time.Sleep(time.Duration(1) * time.Second)
        }
    }
}

一旦信号发送到频道c,非默认情况就会变为非阻滞,并获取选择 ed,函数结束。

这是程序的一个可能输出(在该特定执行期间,我在大约4秒后点击^c):

boop
boop
boop
boop
^CTerminating slowly...

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 a select, you no longer need to start another goroutine; use a select 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:

Package signal will not block sending to c: the caller must ensure that c has sufficient buffer space to keep up with the expected signal rate. For a channel used for notification of just one signal value, a buffer of size 1 is sufficient.

package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {
    c := make(chan os.Signal, 1) // buffered
    signal.Notify(c, os.Interrupt)
    for {
        select {
        case <-c:
            fmt.Println("Terminating slowly...")
            time.Sleep(time.Duration(5) * time.Second)
            return
        default:
            fmt.Println("boop")
            time.Sleep(time.Duration(1) * time.Second)
        }
    }
}

As soon as a signal is sent to channel c, the non-default case becomes non-blocking and gets selected, and the function ends.

Here is one possible output of the program (during that specific execution, I hit ^C after about 4 seconds):

boop
boop
boop
boop
^CTerminating slowly...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文