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

请看下面的例子。

游乐场链接

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()方法时它开始运行。for{}然后它在 goroutine 中触发一个无休止的运行循环。很好,但我也希望能够停止这个过程。我真的不知道如何实现这一目标。非常感谢任何帮助。


泛舟湖上清波郎朗
浏览 136回答 3
3回答

慕的地10843

您可以使用上下文来获取超时和取消,而不需要任何额外的 API。type runner struct{}func (r *runner) StartProcessing(ctx context.Context) {&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <-ctx.Done():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("doing stuff")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(1 * time.Second)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()}这使您可以灵活地设置超时或随时取消它。您还可以利用现有的上下文,这些上下文可能希望在您不知情的情况下更快地超时或取消。// normal timeout after 10 secondsctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)defer cancel()run.StartProcessing(ctx)// decide to cancel earlytime.Sleep(3 * time.Second)cancel()

慕神8447489

你可能会尝试这样的事情......你可能不需要原子,但这有效。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time"&nbsp; &nbsp; "sync/atomic")var trip = int64(0)type runner struct{}func (r *runner) StopProcessing() {&nbsp; &nbsp; atomic.AddInt64(&trip, 1)}func (r *runner) StartProcessing() {&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if trip > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("doing stuff")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(1 * time.Second)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()}func newRunner() *runner {&nbsp; &nbsp; return &runner{}}func main() {&nbsp; &nbsp; run := newRunner()&nbsp; &nbsp; run.StartProcessing()&nbsp;&nbsp; &nbsp; // now wait 4 seconds and the kill the process&nbsp; &nbsp; time.Sleep(4 * time.Second)&nbsp; &nbsp; run.StopProcessing()}

慕沐林林

通过使用通道来发出何时中断 goroutine 的信号。您的代码的相关部分看起来像这样type runner struct {&nbsp; &nbsp; stop chan bool}func (r *runner) StopProcessing() {&nbsp; &nbsp; r.stop <- true}func (r *runner) StartProcessing() {&nbsp; &nbsp; r.stop = make(chan bool)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("doing stuff")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(1 * time.Second)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case _ = <-r.stop:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; close(r.stop)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()}您可以在此处查看完整示例https://play.golang.org/p/OUn18Cprs0I
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go