繁体   English   中英

如何通过调用struct方法来启动/停止功能

[英]How to start/stop a feature by calling a struct method

请参见以下示例。

游乐场链接

type runner struct{}

func (r *runner) StopProcessing() {
    // how to stop?
}

func (r *runner) StartProcessing() {

    go func() {
        for {
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
        }
    }()

}

如您所见,我有一个执行内容的结构,它正在“运行”。 当我调用run.StartProcessing()方法时,它开始运行。 然后,它会在goroutine中触发for{} -loop的无尽运行。 很好,但我也希望能够停止此过程。 我真的不知道如何实现这一目标。 非常感谢您的帮助。

您可以使用上下文来获取超时和取消,而无需任何额外的API。

type runner struct{}

func (r *runner) StartProcessing(ctx context.Context) {
    go func() {
        for {
            select {
            case <-ctx.Done():
                return
            default:
            }
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
        }
    }()
}

这使您可以灵活地设置超时或随时取消超时。 您还可以利用现有的上下文,这些上下文可能要在您不知情的情况下尽快超时或取消。

// normal timeout after 10 seconds
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
run.StartProcessing(ctx)

// decide to cancel early
time.Sleep(3 * time.Second)
cancel()

通过使用通道发出何时发出goroutine信号。 您的代码的相关部分看起来像这样

type runner struct {
    stop chan bool
}

func (r *runner) StopProcessing() {
    r.stop <- true
}

func (r *runner) StartProcessing() {
    r.stop = make(chan bool)
    go func() {
        for {
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
            select {
            case _ = <-r.stop:
                close(r.stop)
                return
            default:
            }
        }
    }()
}

您可以在这里查看完整的示例https://play.golang.org/p/OUn18Cprs0I

您可能会尝试类似的事情……您可能不需要原子,但这可以工作。

package main

import (
    "fmt"
    "time"
    "sync/atomic"
)

var trip = int64(0)

type runner struct{}

func (r *runner) StopProcessing() {
    atomic.AddInt64(&trip, 1)
}

func (r *runner) StartProcessing() {
    go func() {
        for {
            if trip > 0 {
                break
            }
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
        }
    }()
}

func newRunner() *runner {
    return &runner{}
}

func main() {
    run := newRunner()
    run.StartProcessing() 

    // now wait 4 seconds and the kill the process
    time.Sleep(4 * time.Second)
    run.StopProcessing()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM