1

我想在 App Engine 中并行运行 2 个 goroutine,这样当第一个 goroutine 完成其工作时,处理程序不需要等待第二个 goroutine - 它停止第二个 goroutine 并将结果返回给客户端。这可能吗?我用 context.WithCancel() 尝试过,但没有用(我使用 go1.6)。

这是我的代码:

package mytest

import (
    "net/http"
    "sync"
    "time"

    "golang.org/x/net/context"
    "google.golang.org/appengine"
    "google.golang.org/appengine/log"
    "google.golang.org/appengine/urlfetch"
)

func init() {
    http.HandleFunc("/test", handlerTest)
    http.HandleFunc("/testwait10s", handlerTest10s)
    http.HandleFunc("/testwait5s", handlerTest5s)
}

func handlerTest(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    ctx, _ := context.WithTimeout(c, 30*time.Second)

    ctx1, ctx1Cancel := context.WithCancel(ctx)
    ctx2, ctx2Cancel := context.WithCancel(ctx)

    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        defer wg.Done()
        log.Infof(ctx1, "Go1 begin ...")
        client1 := urlfetch.Client(ctx1)
        _, err := client1.Get("http://APP_NAME.appspot.com/testwait5s")
        if err != nil {
            log.Errorf(ctx1, "Go1 failed:  %v", err)
        }
        ctx2Cancel()
        log.Infof(ctx1, "Go1 over ...")
    }()

    go func() {
        defer wg.Done()
        log.Infof(ctx2, "Go2 begin ...")
        client2 := urlfetch.Client(ctx2)
        _, err := client2.Get("http://APP_NAME.appspot.com/testwait10s")
        if err != nil {
            log.Errorf(ctx2, "Go2 failed %v", err)
        }
        ctx1Cancel()
        log.Infof(ctx2, "Go2 over ...")
    }()

    wg.Wait()
    log.Infof(ctx1, "Go1 and GO2 over")
}

func handlerTest10s(w http.ResponseWriter, r *http.Request) {
    time.Sleep(10 * time.Second)
    return
}

func handlerTest5s(w http.ResponseWriter, r *http.Request) {
    time.Sleep(5 * time.Second)
    return
}

有任何想法吗?谢谢!

4

2 回答 2

1

只需创建一个通知通道并向那里发送一个计算结束的信号,您就可以继续进行而无需等待另一个。

func handlerTest(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    ctx, cancel := context.WithTimeout(c, 30*time.Second)
    done := make(chan error, 2)

    work := func(url, name string) {
        log.Infof(ctx, "%s begin ...", name)
        client := urlfetch.Client(ctx)
        req, err := http.NewRequest(http.MethodGet, url, nil)
        if err != nil {
            log.Errorf(ctx, "%s failed:  %v", name, err)
            done <- err
            return
        }
        req = req.WithContext(ctx)
        _, err = client.Do(req)
        done <- err
        if err != nil {
            log.Errorf(ctx, "%s failed:  %v", name, err)
            return
        }
        cancel()
        log.Infof(ctx, "%s over ...", name)
    }
    go work("go1", "http://APP_NAME.appspot.com/testwait5s")
    go work("go2", "http://APP_NAME.appspot.com/testwait10s")

    for i := 0; i < cap(done); i++ {
        if err := <-done; err == nil {
            log.Infof(ctx, "executed without errors")
            return
        }
    }
    log.Error(ctx, "both computations have failed")
}
于 2018-04-03T12:10:36.367 回答
0

您可以尝试减小wg.Add()towg.Add(1)而不是wg.Add(2).

当一个 go-routine 完成时,wg.Done()会将计数器值减少1. 因此,在这种情况下,WaitGroup (wg)计数器值将变为ZERO。结果,wg.Wait()在最后一行,不会等待其他 go-routines 完成。

请注意,如果wgcounter 的值低于零,它将使剩余的 go-routines 恐慌。因此,go-routines 将被强制退出。

于 2018-04-03T11:32:22.663 回答