Running several GET requests parallel

30 views
Skip to first unread message

Vincent Van Mulders

unread,
Dec 1, 2016, 11:20:42 AM12/1/16
to google-appengine-go
I'm trying to use goroutines to perform several http requests to a third party API

I read this: 

The Go runtime environment for App Engine provides full support for goroutines, but not for parallel execution: goroutines are scheduled onto a single operating system thread. This single-thread restriction may be lifted in future versions.


Is this possible in GAE or is http requests using goroutines/parallel impossible with the current runtime?

Trying to use the *http.Request in a goroutine gives me "appengine: NewContext passed an unkown http.Request


Chris Broadfoot

unread,
Dec 1, 2016, 11:24:33 AM12/1/16
to Vincent Van Mulders, google-appengine-go
Yes, it's possible. You need to be within the context of an incoming request, though.

Something like this:

func handler(w http.ResponseWriter, r *http.Request) {
  ctx := appengine.NewContext(r)
  hc := urlfetch.Client(ctx)
  var wg sync.WaitGroup
  wg.Add(10)
  for i := 0; i < 10; i++ {
    go func() {
      resp, err := hc.Get(...)
      _, _ = resp, err
      wg.Done()
    }()
  }
  wg.Wait()
  io.WriteString(w, "10 requests done")
}

--
You received this message because you are subscribed to the Google Groups "google-appengine-go" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine-go+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages