2014-09-05 12:13
浏览 35

Golang,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


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 ?


图片转代码服务由CSDN问答提供 功能建议


”“尽管goroutine和通道是 当前,当Go应用在App Engine上运行时,在给定实例中仅运行一个线程,也就是说,所有goroutine在单个操作系统线程中运行,因此对于给定的客户端请求没有可用的CPU并行性。 将会在某个时候被解除。”


我有一个仅能利用优势的应用程序 Golang的速度; 接收一些输入数据,对其进行内存计算,然后返回结果。 永远不要接触数据存储区或任何其他App Engine API。

我需要让该应用执行许多计算,理想情况下需要进行一定程度的并行化。 因为我是Golang菜鸟,所以我只是在没有考虑线程安全的情况下对算法进行了编码。 当我一次发送一个请求时,这种方法工作正常,但是当我尝试并行发送多个计算时,所有结果都是错误的。 我怀疑但不是100%知道线程安全是问题,尤其是因为算法使用的是地图,而地图也不是线程安全的


如此。 如何使我的算法线程安全并获得一定程度的并行性。


是否可以使用Go的 缓冲通道作为线程安全队列?



有人可以启发我 在Golang App Engine上实现一定程度的线程安全并行化的最佳模式?


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsfs1233 2014-09-05 13:30

    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

    解决 无用
    打赏 举报
  • doushou6480 2014-09-05 13:27

    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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题