Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 戈罗廷没有';不要尊重'ctx.done()`或正确退出_Go - Fatal编程技术网

Go 戈罗廷没有';不要尊重'ctx.done()`或正确退出

Go 戈罗廷没有';不要尊重'ctx.done()`或正确退出,go,Go,当用户按下Ctrl-C时,我试图优雅地退出。我正在尝试输入代码。 包干管 import ( "context" "fmt" "os" "os/signal" "time" ) func main() { ctx := context.Background() // trap Ctrl+C and call cancel on the context ctx, cancel := context.WithCancel(ctx)

当用户按下
Ctrl-C
时,我试图优雅地退出。我正在尝试输入代码。 包干管

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

func main() {

    ctx := context.Background()

    // trap Ctrl+C and call cancel on the context
    ctx, cancel := context.WithCancel(ctx)
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    defer func() {
        signal.Stop(c)
        cancel()
        fmt.Println("Cleaned up")
    }()
    go func() {
        select {
        case <-c:
            fmt.Println("Got interrupt signal")
            cancel()
        case <-ctx.Done():
        }
        fmt.Println("Stopped monitoring")
    }()
    select {
    case <-ctx.Done():
        fmt.Println("notified to quit")
    case <-time.NewTimer(time.Second * 2).C:
        fmt.Println("done something")
    }
}
但是,如果正常退出,则无法按预期工作,如下所示:

done something
Cleaned up
我的意思是它应该打印出停止监视的
,但不是。在
延迟清理
函数中,它调用了
cancel()
,这将触发监控goroutine中的
选择退出,但不会


如何解决这个问题

谢谢@Zan Lynx,我想出了下面的解决方案

package main

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

func main() {
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    terminated := monitor(ctx, cancel)
    defer func() {
        cancel()
        fmt.Println("Cleaned up")
        <-terminated // wait for the monior goroutine quit
    }()
    select {
    case <-ctx.Done():
        fmt.Println("notified to quit")
    case <-time.NewTimer(time.Second * 1).C:
        fmt.Println("done something")
    }
}

func monitor(ctx context.Context, cancel context.CancelFunc) <-chan interface{} {
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    terminated := make(chan interface{})
    go func() {
        defer close(terminated)
        defer fmt.Println("Stopped monitoring1")
        defer signal.Stop(c)
        select {
        case <-c:
            fmt.Println("Got interrupt singnal")
            cancel()
        case <-ctx.Done():
        }
    }()
    return terminated
}
主程序包
进口(
“上下文”
“fmt”
“操作系统”
“操作系统/信号”
“时间”
)
func main(){
ctx:=context.Background()
ctx,cancel:=上下文。带取消(ctx)
终止:=监视器(ctx,取消)
延迟函数(){
取消
fmt.Println(“已清理”)

您不必等待取消。您只需退出main,点击defer,然后在goroutine运行之前蒸发程序。非常正确,它没有等待goroutine退出。我修改了代码并将其作为答案发布。
package main

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

func main() {
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    terminated := monitor(ctx, cancel)
    defer func() {
        cancel()
        fmt.Println("Cleaned up")
        <-terminated // wait for the monior goroutine quit
    }()
    select {
    case <-ctx.Done():
        fmt.Println("notified to quit")
    case <-time.NewTimer(time.Second * 1).C:
        fmt.Println("done something")
    }
}

func monitor(ctx context.Context, cancel context.CancelFunc) <-chan interface{} {
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    terminated := make(chan interface{})
    go func() {
        defer close(terminated)
        defer fmt.Println("Stopped monitoring1")
        defer signal.Stop(c)
        select {
        case <-c:
            fmt.Println("Got interrupt singnal")
            cancel()
        case <-ctx.Done():
        }
    }()
    return terminated
}