duanjian4331
duanjian4331
2014-09-05 12:13

Golang,App Engine,渠道和线程安全

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

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.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐