0

http://blog.golang.org/go-and-google-app-engine

"Also, although goroutines and channels are present, when a Go app runs on App Engine only one thread is run in a given instance. That is, all goroutines run in a single operating system thread, so there is no CPU parallelism available for a given client request. We expect this restriction will be lifted at some point"

That was in May 2011. Is it still true ?

I have an app which does nothing but take advantage of Golang's speed; takes some input data, performs an in- memory calculation on it, returns the results. Never touches the datastore or any other App Engine APIs.

I need to the app to perform many calculations, ideally with some degree of parallelisation. Because I am a Golang noob I just coded up the algo without any thought to thread safety. This worked fine when I sent one request at a time, but when I tried sending multiple calculations in parallel, all the results were wrong. I suspect, but don't know 100%, that thread safety is the problem, esp as the algo uses maps and maps are not thread safe

http://golang.org/doc/faq#atomic_maps

So. How to make my algo thread safe and get some degree of parallelism.

First thoughts was to use channels, which seem to be thread safe:

Is it possible to use Go's buffered channel as a thread-safe queue?

But then I came across the link at the top which suggests channels might not be available.

So .. if they are not available, maybe I need to set up a task queue for the calculation which can only perform one calculation at a time.

Can someone enlighten me on the best pattern for achieving a degree of thread- safe parallelisation on Golang App Engine ?

Thanks.

Community
  • 1
  • 1
Justin
  • 4,649
  • 6
  • 33
  • 71
  • Don't know if this is it, but any global variables can still be updated by multiple goroutines, even with GOMAXPROCS=1. If you don't really need globals to coordinate multiple tasks, make them not global anymore -- maybe move them into structs that you allocate per-request. The question's a pretty general description, so it's hard to say more. – twotwotwo Sep 05 '14 at 18:23

2 Answers2

1

Google Appengine only allows one OS thread at the moment.

To understand how OS Threads; Goroutines, and their stacks; and the scheduler work, I recommend reading the Scalable Go Scheduler proposal or the Analysis of the Go Runtime Scheduler.

Since you can have multiple channels, goroutines on a single thread, Appengine will do fine with concurrency. If you need parallelism (i.e. running not only multiple goroutines, but running them over many processors), then Appengine is not the answer at the moment as its GOMAXPROCS is set to one.

As far as your specific code is concerned, you've provided none that we can look at and help debug your race condition. To do so yourself, you'd benefit from reading this blog post about, and using, the Go Race Detector

Momer
  • 3,158
  • 22
  • 23
0

I don't know if app engine currently supports multiple operating system threads, but you can absolutely use channels even when only one OS-level thread is available. The link you provide does state "goroutines and channels are present", they're just all handled in one OS-level thread.

Evan
  • 6,369
  • 1
  • 29
  • 30